robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
0639f6e41a
master
1" tar.vim: Handles browsing tarfiles - AUTOLOAD PORTION 2" Date: Mar 01, 2025 3" Version: 32b (with modifications from the Vim Project) 4" Maintainer: This runtime file is looking for a new maintainer. 5" Former Maintainer: Charles E Campbell 6" License: Vim License (see vim's :help license) 7" Last Change: 8" 2024 Jan 08 by Vim Project: fix a few problems (#138331, #12637, #8109) 9" 2024 Feb 19 by Vim Project: announce adoption 10" 2024 Nov 11 by Vim Project: support permissions (#7379) 11" 2025 Feb 06 by Vim Project: add support for lz4 (#16591) 12" 2025 Feb 28 by Vim Project: add support for bzip3 (#16755) 13" 2025 Mar 01 by Vim Project: fix syntax error in tar#Read() 14" 2025 Mar 02 by Vim Project: escape the filename before using :read 15" 2025 Mar 02 by Vim Project: determine the compression using readblob() 16" instead of shelling out to file(1) 17" 2025 Apr 16 by Vim Project: decouple from netrw by adding s:WinPath() 18" 2025 May 19 by Vim Project: restore working directory after read/write 19" 2025 Jul 13 by Vim Project: warn with path traversal attacks 20" 2026 Feb 06 by Vim Project: consider 'nowrapscan' (#19333) 21" 2026 Feb 07 by Vim Project: make the path traversal detection more robust (#19341) 22" 2026 Apr 06 by Vim Project: fix bugs with lz4 support (#19925) 23" 2026 Apr 09 by Vim Project: fix bugs with zstd support (#19930) 24" 2026 Apr 09 by Vim Project: fix bug with dotted filename (#19930) 25" 2026 Apr 15 by Vim Project: fix more path traversal issues (#19981) 26" 2026 Apr 16 by Vim Project: use g:tar_secure in tar#Extract() 27" 2026 May 14 by Vim Project: use correct shellescape() call in Vimuntar() 28" 2026 Jun 16 by Vim Project: fix lz4 extraction on non-linux systemd (#20555) 29" 30" Contains many ideas from Michael Toren's <tar.vim> 31" 32" Copyright: Copyright (C) 2005-2017 Charles E. Campbell {{{1 33" Permission is hereby granted to use and distribute this code, 34" with or without modifications, provided that this copyright 35" notice is copied with it. Like anything else that's free, 36" tar.vim and tarPlugin.vim are provided *as is* and comes 37" with no warranty of any kind, either expressed or implied. 38" By using this plugin, you agree that in no event will the 39" copyright holder be liable for any damages resulting from 40" the use of this software. 41" --------------------------------------------------------------------- 42" Load Once: {{{1 43if & cp || exists ( "g:loaded_tar" ) 44finish 45endif 46let g: loaded_tar = "v32b" 47if ! has ( 'nvim-0.12' ) && v: version < 900 48echohl WarningMsg 49echo "***warning*** this version of tar needs vim 9.0" 50echohl Normal 51finish 52endif 53let s: keepcpo = & cpo 54set cpo & vim 55 56" --------------------------------------------------------------------- 57" Default Settings: {{{1 58if ! exists ( "g:tar_browseoptions" ) 59let g: tar_browseoptions = "tf" 60endif 61if ! exists ( "g:tar_readoptions" ) 62let g: tar_readoptions = "pxf" 63endif 64if ! exists ( "g:tar_cmd" ) 65let g: tar_cmd = "tar" 66endif 67if ! exists ( "g:tar_writeoptions" ) 68let g: tar_writeoptions = "uf" 69endif 70if ! exists ( "g:tar_delfile" ) 71" Note: not supported on BSD 72let g: tar_delfile = "--delete -f" 73endif 74if ! exists ( "g:netrw_cygwin" ) 75if has ( "win32" ) || has ( "win95" ) || has ( "win64" ) || has ( "win16" ) 76if & shell =~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$' 77let g: netrw_cygwin = 1 78else 79let g: netrw_cygwin = 0 80endif 81else 82let g: netrw_cygwin = 0 83endif 84endif 85if ! exists ( "g:tar_copycmd" ) 86if ! exists ( "g:netrw_localcopycmd" ) 87if has ( "win32" ) || has ( "win95" ) || has ( "win64" ) || has ( "win16" ) 88if g: netrw_cygwin 89let g: netrw_localcopycmd = "cp" 90else 91let g: netrw_localcopycmd = "copy" 92endif 93elseif has ( "unix" ) || has ( "macunix" ) 94let g: netrw_localcopycmd = "cp" 95else 96let g: netrw_localcopycmd = "" 97endif 98endif 99let g: tar_copycmd = g: netrw_localcopycmd 100endif 101if ! exists ( "g:tar_extractcmd" ) 102let g: tar_extractcmd = "tar -pxf" 103endif 104 105" set up shell quoting character 106if ! exists ( "g:tar_shq" ) 107if exists ( "+shq" ) && exists ( "&shq" ) && & shq != "" 108let g: tar_shq = & shq 109elseif has ( "win32" ) || has ( "win95" ) || has ( "win64" ) || has ( "win16" ) 110if exists ( "g:netrw_cygwin" ) && g: netrw_cygwin 111let g: tar_shq = "'" 112else 113let g: tar_shq = '"' 114endif 115else 116let g: tar_shq = "'" 117endif 118endif 119 120let g: tar_secure = ' -- ' 121let g: tar_leading_pat = '\m^\%([.]\{,2\}/\)\+' 122 123" ---------------- 124" Functions: {{{1 125" ---------------- 126 127" --------------------------------------------------------------------- 128" s:Msg: {{{2 129fun ! s: Msg ( func , severity , msg ) 130redraw ! 131if a: severity =~? 'error' 132echohl Error 133else 134echohl WarningMsg 135endif 136echo $ "***{a:severity}*** ({a:func}) {a:msg}" 137echohl None 138endfunc 139 140" --------------------------------------------------------------------- 141" tar#Browse: {{{2 142fun ! tar#Browse ( tarfile ) 143let repkeep = & report 144set report = 10 145 146" sanity checks 147if ! executable ( g: tar_cmd ) 148call s: Msg ( 'tar#Browse' , 'error' , $ "{g: tar_cmd } not available on your system") 149let & report = repkeep 150return 151endif 152if ! filereadable ( a: tarfile ) 153if a: tarfile !~# '^\a\+://' 154" if it's an url, don't complain, let url-handlers such as vim do its thing 155call s: Msg ( 'tar#Browse' , 'error' , $ "File not readable<{a:tarfile}>") 156endif 157let & report = repkeep 158return 159endif 160if & ma != 1 161set ma 162endif 163let b: tarfile = a: tarfile 164 165setlocal noswapfile 166setlocal buftype = nofile 167setlocal bufhidden = hide 168setlocal nobuflisted 169setlocal nowrap 170set ft = tar 171 172" give header 173let lastline = line ( "$" ) 174call setline ( lastline + 1 , '" tar.vim version ' . g: loaded_tar ) 175call setline ( lastline + 2 , '" Browsing tarfile ' . a: tarfile ) 176call setline ( lastline + 3 , '" Select a file with cursor and press ENTER, "x" to extract a file' ) 177keepj $put ='' 178keepj sil! 0d 179keepj $ 180 181let tarfile = a: tarfile 182if has ( "win32unix" ) && executable ( "cygpath" ) 183" assuming cygwin 184let tarfile = substitute ( system ( "cygpath -u " . shellescape ( tarfile , 0 )), '\n$' , '' , 'e' ) 185endif 186let curlast = line ( "$" ) 187 188if tarfile =~# '\.\(gz\)$' 189exe "sil! r! gzip -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 190 191elseif tarfile =~# '\.\(tgz\)$' || tarfile =~# '\.\(tbz\)$' || tarfile =~# '\.\(txz\)$' || 192 \tarfile =~# '\.\(tzst\)$' || tarfile =~# '\.\(tlz4\)$' 193let header = s: Header ( tarfile ) 194 195if header =~? 'bzip2' 196exe "sil! r! bzip2 -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 197elseif header =~? 'bzip3' 198exe "sil! r! bzip3 -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 199elseif header =~? 'xz' 200exe "sil! r! xz -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 201elseif header =~? 'zstd' 202exe "sil! r! zstd --decompress --stdout -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 203elseif header =~? 'lz4' 204exe "sil! r! lz4 --decompress --stdout -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 205elseif header =~? 'gzip' 206exe "sil! r! gzip -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 207endif 208 209elseif tarfile =~# '\.lrp' 210exe "sil! r! cat -- " . shellescape ( tarfile , 1 ) . "|gzip -d -c -|" . g: tar_cmd . " -" . g: tar_browseoptions . " - " 211elseif tarfile =~# '\.\(bz2\|tbz\|tb2\)$' 212exe "sil! r! bzip2 -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 213elseif tarfile =~# '\.\(bz3\|tb3\)$' 214exe "sil! r! bzip3 -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 215elseif tarfile =~# '\.\(lzma\|tlz\)$' 216exe "sil! r! lzma -d -c -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 217elseif tarfile =~# '\.\(xz\|txz\)$' 218exe "sil! r! xz --decompress --stdout -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 219elseif tarfile =~# '\.\(zst\|tzst\)$' 220exe "sil! r! zstd --decompress --stdout -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 221elseif tarfile =~# '\.\(lz4\|tlz4\)$' 222exe "sil! r! lz4 --decompress --stdout -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_browseoptions . " - " 223else 224if tarfile =~ '^\s*-' 225" A file name starting with a dash is taken as an option. Prepend ./ to avoid that. 226let tarfile = substitute ( tarfile , '-' , './-' , '' ) 227endif 228exe "sil! r! " . g: tar_cmd . " -" . g: tar_browseoptions . " " . shellescape ( tarfile , 1 ) 229endif 230if v: shell_error != 0 231call s: Msg ( 'tar#Browse' , 'warning' , $ "please check yourg: tar_browseoptions '<{g: tar_browseoptions } >'") 232return 233endif 234 235" remove tar: Removing leading '/' from member names 236" Note: the message could be localized 237if search ( '\m^g\?tar: ' , 'w' ) > 0 || search ( g: tar_leading_pat , 'w' ) > 0 238call append ( 3 , '" Note: Path Traversal Attack detected!' ) 239let b: leading_slash = 1 240" remove the message output 241sil g /^tar: /d 242endif 243 244" set up maps supported for tar 245setlocal noma nomod ro 246noremap <silent> <buffer> < cr > :call< SID > TarBrowseSelect()< cr > 247noremap <silent> <buffer> x :call tar#Extract()< cr > 248if & mouse != "" 249noremap <silent> <buffer> < leftmouse > < leftmouse > :call< SID > TarBrowseSelect()< cr > 250endif 251 252let & report = repkeep 253endfun 254 255" --------------------------------------------------------------------- 256" TarBrowseSelect: {{{2 257fun ! s: TarBrowseSelect () 258let repkeep = & report 259set report = 10 260let fname = getline ( "." ) 261let ls = get ( b:, 'leading_slash' , 0 ) 262 263" sanity check 264if fname =~ '^"' 265let & report = repkeep 266return 267endif 268 269" about to make a new window, need to use b:tarfile 270let tarfile = b: tarfile 271let curfile = expand ( "%" ) 272if has ( "win32unix" ) && executable ( "cygpath" ) 273" assuming cygwin 274let tarfile = substitute ( system ( "cygpath -u " . shellescape ( tarfile , 0 )), '\n$' , '' , 'e' ) 275endif 276 277" open a new window (tar#Read will read a file into it) 278noswapfile new 279if ! exists ( "g:tar_nomax" ) || g: tar_nomax == 0 280wincmd _ 281endif 282let s: tblfile_ { winnr ()} = curfile 283let b: leading_slash = ls 284call tar#Read ( "tarfile:" . tarfile . '::' . fname ) 285filetype detect 286set nomod 287exe 'com! -buffer -nargs=? -complete=file TarDiff :call tar#Diff(<q-args>,"' . fnameescape ( fname ) . '")' 288 289let & report = repkeep 290endfun 291 292" --------------------------------------------------------------------- 293" tar#Read: {{{2 294fun ! tar#Read ( fname ) 295let repkeep = & report 296set report = 10 297let tarfile = substitute ( a: fname , 'tarfile:\(.\{-}\)::.*$' , '\1' , '' ) 298let fname = substitute ( a: fname , 'tarfile:.\{-}::\(.*\)$' , '\1' , '' ) 299" be careful not to execute special crafted files 300let escape_file = fname ->substitute ( g: tar_leading_pat , '' , '' ) ->fnameescape () 301 302let curdir = getcwd () 303let b: curdir = curdir 304let tmpdir = tempname () 305let b: tmpdir = tmpdir 306if tmpdir =~ '\.' 307let tmpdir = substitute ( tmpdir , '\.[^.]*$' , '' , 'e' ) 308endif 309call mkdir ( tmpdir , "p" ) 310 311" attempt to change to the indicated directory 312try 313exe "lcd " . fnameescape ( tmpdir ) 314catch /^Vim\%((\a\+)\)\=:E344/ 315call s: Msg ( 'tar#Read' , 'error' , "cannot lcd to temporary directory" ) 316let & report = repkeep 317return 318endtry 319 320" place temporary files under .../_ZIPVIM_/ 321if isdirectory ( "_ZIPVIM_" ) 322call s: Rmdir ( "_ZIPVIM_" ) 323endif 324call mkdir ( "_ZIPVIM_" ) 325lcd _ZIPVIM_ 326 327if has ( "win32unix" ) && executable ( "cygpath" ) 328" assuming cygwin 329let tarfile = substitute ( system ( "cygpath -u " . shellescape ( tarfile , 0 )), '\n$' , '' , 'e' ) 330endif 331 332if fname =~ '\.bz2$' && executable ( "bzcat" ) 333let decmp = "|bzcat" 334let doro = 1 335elseif fname =~ '\.bz3$' && executable ( "bz3cat" ) 336let decmp = "|bz3cat" 337let doro = 1 338elseif fname =~ '\.t\=gz$' && executable ( "zcat" ) 339let decmp = "|zcat" 340let doro = 1 341elseif fname =~ '\.lzma$' && executable ( "lzcat" ) 342let decmp = "|lzcat" 343let doro = 1 344elseif fname =~ '\.xz$' && executable ( "xzcat" ) 345let decmp = "|xzcat" 346let doro = 1 347elseif fname =~ '\.zst$' && executable ( "zstdcat" ) 348let decmp = "|zstdcat" 349let doro = 1 350elseif fname =~ '\.lz4$' && executable ( "lz4cat" ) 351let decmp = "|lz4cat" 352let doro = 1 353else 354let decmp = "" 355let doro = 0 356if fname =~ '\.bz2$\|\.bz3$\|\.gz$\|\.lzma$\|\.xz$\|\.zip$\|\.Z$' 357setlocal bin 358endif 359endif 360 361 362if tarfile =~# '\.bz2$' 363exe "sil! r! bzip2 -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 364exe "read " . escape_file 365elseif tarfile =~# '\.bz3$' 366exe "sil! r! bzip3 -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 367exe "read " . escape_file 368elseif tarfile =~# '\.\(gz\)$' 369exe "sil! r! gzip -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 370exe "read " . escape_file 371elseif tarfile =~# '\(\.tgz\|\.tbz\|\.txz\)' 372let filekind = s: Header ( tarfile ) 373if filekind =~? "bzip2" 374exe "sil! r! bzip2 -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 375exe "read " . escape_file 376elseif filekind =~ "bzip3" 377exe "sil! r! bzip3 -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 378exe "read " . escape_file 379elseif filekind =~? "xz" 380exe "sil! r! xz -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 381exe "read " . escape_file 382elseif filekind =~? "zstd" 383exe "sil! r! zstd --decompress --stdout -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 384exe "read " . escape_file 385elseif filekind =~? "gzip" 386exe "sil! r! gzip -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 387exe "read " . escape_file 388endif 389 390elseif tarfile =~# '\.lrp$' 391exe "sil! r! cat -- " . shellescape ( tarfile , 1 ) . " | gzip -d -c - | " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 392exe "read " . escape_file 393elseif tarfile =~# '\.lzma$' 394exe "sil! r! lzma -d -c -- " . shellescape ( tarfile , 1 ) . "| " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 395exe "read " . escape_file 396elseif tarfile =~# '\.\(xz\|txz\)$' 397exe "sil! r! xz --decompress --stdout -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 398exe "read " . escape_file 399elseif tarfile =~# '\.\(lz4\|tlz4\)$' 400exe "sil! r! lz4 --decompress --stdout -- " . shellescape ( tarfile , 1 ) . " | " . g: tar_cmd . " -" . g: tar_readoptions . " - " . g: tar_secure . shellescape ( fname , 1 ). decmp 401exe "read " . escape_file 402else 403if tarfile =~ '^\s*-' 404" A file name starting with a dash is taken as an option. Prepend ./ to avoid that. 405let tarfile = substitute ( tarfile , '-' , './-' , '' ) 406endif 407exe "silent r! " . g: tar_cmd . " -" . g: tar_readoptions . shellescape ( tarfile , 1 ) . " " . g: tar_secure . shellescape ( fname , 1 ). decmp 408exe "read " . escape_file 409endif 410if get ( b:, 'leading_slash' , 0 ) 411sil g /^tar: /d 412endif 413 414redraw ! 415 416if v: shell_error != 0 417lcd .. 418call s: Rmdir ( "_ZIPVIM_" ) 419exe "lcd " . fnameescape ( curdir ) 420call s: Msg ( 'tar#Read' , 'error' , $ "sorry, unable to open or extract {tarfile} with {fname}") 421endif 422 423if doro 424" because the reverse process of compressing changed files back into the tarball is not currently supported 425setlocal ro 426endif 427 428let b: tarfile = a: fname 429 430" cleanup 431keepj sil! 0d 432set nomod 433 434let & report = repkeep 435exe "lcd " . fnameescape ( curdir ) 436silent exe "file tarfile::" . fname ->fnameescape () 437endfun 438 439" --------------------------------------------------------------------- 440" tar#Write: {{{2 441fun ! tar#Write ( fname ) 442let pwdkeep = getcwd () 443let repkeep = & report 444set report = 10 445let curdir = b: curdir 446let tmpdir = b: tmpdir 447 448" sanity checks 449if ! executable ( g: tar_cmd ) 450redraw ! 451let & report = repkeep 452return 453endif 454let tarfile = substitute ( b: tarfile , 'tarfile:\(.\{-}\)::.*$' , '\1' , '' ) 455let fname = substitute ( b: tarfile , 'tarfile:.\{-}::\(.*\)$' , '\1' , '' ) 456 457if get ( b:, 'leading_slash' , 0 ) 458call s: Msg ( 'tar#Write' , 'error' , $ "sorry, not attempting to update {tarfile} with {fname}") 459let & report = repkeep 460return 461endif 462 463if ! isdirectory ( fnameescape ( tmpdir )) 464call mkdir ( fnameescape ( tmpdir ), 'p' ) 465endif 466exe $ "lcd {fnameescape(tmpdir)}" 467if isdirectory ( "_ZIPVIM_" ) 468call s: Rmdir ( "_ZIPVIM_" ) 469endif 470call mkdir ( "_ZIPVIM_" ) 471lcd _ZIPVIM_ 472let dir = fnamemodify ( fname , ':p:h' ) 473if dir !~# '_ZIPVIM_$' 474call mkdir ( dir ) 475endif 476 477" handle compressed archives 478if tarfile =~# '\.bz2' 479call system ( "bzip2 -d -- " . shellescape ( tarfile , 0 )) 480let tarfile = substitute ( tarfile , '\.bz2' , '' , 'e' ) 481let compress = "bzip2 -- " . shellescape ( tarfile , 0 ) 482elseif tarfile =~# '\.bz3' 483call system ( "bzip3 -d -- " . shellescape ( tarfile , 0 )) 484let tarfile = substitute ( tarfile , '\.bz3' , '' , 'e' ) 485let compress = "bzip3 -- " . shellescape ( tarfile , 0 ) 486elseif tarfile =~# '\.gz' 487call system ( "gzip -d -- " . shellescape ( tarfile , 0 )) 488let tarfile = substitute ( tarfile , '\.gz' , '' , 'e' ) 489let compress = "gzip -- " . shellescape ( tarfile , 0 ) 490elseif tarfile =~# '\.tgz' 491call system ( "gzip -d -- " . shellescape ( tarfile , 0 )) 492let tarfile = substitute ( tarfile , '\.tgz' , '.tar' , 'e' ) 493let compress = "gzip -- " . shellescape ( tarfile , 0 ) 494let tgz = 1 495elseif tarfile =~# '\.xz' 496call system ( "xz -d -- " . shellescape ( tarfile , 0 )) 497let tarfile = substitute ( tarfile , '\.xz' , '' , 'e' ) 498let compress = "xz -- " . shellescape ( tarfile , 0 ) 499elseif tarfile =~# '\.zst' 500call system ( "zstd --decompress --rm -- " . shellescape ( tarfile , 0 )) 501let tarfile = substitute ( tarfile , '\.zst' , '' , 'e' ) 502let compress = "zstd --rm -- " . shellescape ( tarfile , 0 ) 503elseif tarfile =~# '\.lz4' 504call system ( "lz4 --decompress --rm -- " . shellescape ( tarfile , 0 )) 505let tarfile = substitute ( tarfile , '\.lz4' , '' , 'e' ) 506let compress = "lz4 --rm -- " . shellescape ( tarfile , 0 ) 507elseif tarfile =~# '\.lzma' 508call system ( "lzma -d -- " . shellescape ( tarfile , 0 )) 509let tarfile = substitute ( tarfile , '\.lzma' , '' , 'e' ) 510let compress = "lzma -- " . shellescape ( tarfile , 0 ) 511endif 512" Note: no support for name.tar.tbz/.txz/.tgz/.tlz4/.tzst 513 514if v: shell_error != 0 515call s: Msg ( 'tar#Write' , 'error' , $ "sorry, unable to update {tarfile} with {fname}") 516else 517 518if fname =~ '/' 519let dirpath = substitute ( fname , '/[^/]\+$' , '' , 'e' ) 520if has ( "win32unix" ) && executable ( "cygpath" ) 521let dirpath = substitute ( system ( "cygpath " . shellescape ( dirpath , 0 )), '\n' , '' , 'e' ) 522endif 523call mkdir ( dirpath , "p" ) 524endif 525if tarfile !~ '/' 526let tarfile = curdir . '/' . tarfile 527endif 528if tarfile =~ '^\s*-' 529" A file name starting with a dash may be taken as an option. Prepend ./ to avoid that. 530let tarfile = substitute ( tarfile , '-' , './-' , '' ) 531endif 532 533" don't overwrite a file forcefully 534exe "w " . fnameescape ( fname ) 535if has ( "win32unix" ) && executable ( "cygpath" ) 536let tarfile = substitute ( system ( "cygpath " . shellescape ( tarfile , 0 )), '\n' , '' , 'e' ) 537endif 538 539" delete old file from tarfile 540" Note: BSD tar does not support --delete flag 541call system ( g: tar_cmd . " " . g: tar_delfile . " " . shellescape ( tarfile , 0 ) . g: tar_secure . shellescape ( fname , 0 )) 542if v: shell_error != 0 543call s: Msg ( 'tar#Write' , 'error' , $ "sorry, unable to update {fnameescape(tarfile)} with {fnameescape(fname)} --delete not supported?") 544else 545" update tarfile with new file 546call system ( g: tar_cmd . " -" . g: tar_writeoptions . " " . shellescape ( tarfile , 0 ) . g: tar_secure . shellescape ( fname , 0 )) 547if v: shell_error != 0 548call s: Msg ( 'tar#Write' , 'error' , $ "sorry, unable to update {fnameescape(tarfile)} with {fnameescape(fname)}") 549elseif exists ( "compress" ) 550call system ( compress ) 551if exists ( "tgz" ) 552call rename ( tarfile . ".gz" , substitute ( tarfile , '\.tar$' , '.tgz' , 'e' )) 553endif 554endif 555endif 556 557" support writing tarfiles across a network 558if s: tblfile_ { winnr ()} =~ '^\a\+://' 559let tblfile = s: tblfile_ { winnr ()} 5601 split |noswapfile enew 561let binkeep = & l: binary 562let eikeep = & ei 563set binary ei = all 564exe "noswapfile e! " . fnameescape ( tarfile ) 565call netrw#NetWrite ( tblfile ) 566let & ei = eikeep 567let & l: binary = binkeep 568q ! 569unlet s: tblfile_ { winnr ()} 570endif 571endif 572 573" cleanup and restore current directory 574lcd .. 575call s: Rmdir ( "_ZIPVIM_" ) 576exe "lcd " . fnameescape ( pwdkeep ) 577setlocal nomod 578 579let & report = repkeep 580endfun 581 582" --------------------------------------------------------------------- 583" tar#Diff: {{{2 584fun ! tar#Diff ( userfname , fname ) 585let fname = a: fname 586if a: userfname != "" 587let fname = a: userfname 588endif 589exe "lcd " . fnameescape ( b: tmpdir ) . '/_ZIPVIM_' 590if filereadable ( fname ) 591" sets current file (from tarball) for diff'ing 592" splits window vertically 593" opens original file, sets it for diff'ing 594" sets up b:tardiff_otherbuf variables so each buffer knows about the other (for closing purposes) 595diffthis 596wincmd v 597exe "noswapfile e " . fnameescape ( fname ) 598diffthis 599else 600redraw ! 601echo "***warning*** unable to read file<" . fname . ">" 602endif 603endfun 604 605" --------------------------------------------------------------------- 606" tar#Extract: extract a file from a (possibly compressed) tar archive {{{2 607fun ! tar#Extract () 608 609let repkeep = & report 610set report = 10 611let fname = getline ( "." ) 612 613" sanity check 614if fname =~ '^"' 615let & report = repkeep 616return 617endif 618if fname =~ '^[.]\?[.]/' || simplify ( fname ) =~ '\.\.[/\\]' 619call s: Msg ( 'tar#Extract' , 'error' , "Path Traversal Attack detected, not extracting!" ) 620let & report = repkeep 621return 622endif 623if has ( "unix" ) 624if fname =~ '^/' 625call s: Msg ( 'tar#Extract' , 'error' , "Path Traversal Attack detected, not extracting!" ) 626let & report = repkeep 627return 628endif 629else 630if fname =~ '^\%(\a:[\\/]\|[\\/]\)' 631call s: Msg ( 'tar#Extract' , 'error' , "Path Traversal Attack detected, not extracting!" ) 632let & report = repkeep 633return 634endif 635endif 636 637let extractcmd = s: WinPath ( g: tar_extractcmd ) 638let tarball = expand ( "%" ) 639if ! filereadable ( tarball ) 640let & report = repkeep 641return 642endif 643 644if tarball =~# "\.tar$" 645call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 646if v: shell_error != 0 647call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 648else 649echo "***note*** successfully extracted " . fname 650endif 651 652elseif tarball =~# "\.tgz$" 653let extractcmd = substitute ( extractcmd , "-" , "-z" , "" ) 654call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 655if v: shell_error != 0 656call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 657else 658echo "***note*** successfully extracted " . fname 659endif 660 661elseif tarball =~# "\.tar\.gz$" 662let extractcmd = substitute ( extractcmd , "-" , "-z" , "" ) 663call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 664if v: shell_error != 0 665call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 666else 667echo "***note*** successfully extracted " . fname 668endif 669 670elseif tarball =~# "\.tbz$" 671let extractcmd = substitute ( extractcmd , "-" , "-j" , "" ) 672call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 673if v: shell_error != 0 674call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 675else 676echo "***note*** successfully extracted " . fname 677endif 678 679elseif tarball =~# "\.tar\.bz2$" 680let extractcmd = substitute ( extractcmd , "-" , "-j" , "" ) 681call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 682if v: shell_error != 0 683call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 684else 685echo "***note*** successfully extracted " . fname 686endif 687 688elseif tarball =~# "\.tar\.bz3$" 689let extractcmd = substitute ( extractcmd , "-" , "-j" , "" ) 690call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 691if v: shell_error != 0 692call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 693else 694echo "***note*** successfully extracted " . fname 695endif 696 697elseif tarball =~# "\.txz$" 698let extractcmd = substitute ( extractcmd , "-" , "-J" , "" ) 699call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 700if v: shell_error != 0 701call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 702else 703echo "***note*** successfully extracted " . fname 704endif 705 706elseif tarball =~# "\.tar\.xz$" 707let extractcmd = substitute ( extractcmd , "-" , "-J" , "" ) 708call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 709if v: shell_error != 0 710call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 711else 712echo "***note*** successfully extracted " . fname 713endif 714 715elseif tarball =~# "\.tzst$" 716let extractcmd = substitute ( extractcmd , "-" , "--zstd -" , "" ) 717call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 718if v: shell_error != 0 719call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 720else 721echo "***note*** successfully extracted " . fname 722endif 723 724elseif tarball =~# "\.tar\.zst$" 725let extractcmd = substitute ( extractcmd , "-" , "--zstd -" , "" ) 726call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 727if v: shell_error != 0 728call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 729else 730echo "***note*** successfully extracted " . fname 731endif 732 733elseif tarball =~# "\.tlz4$" 734if has ( "linux" ) 735let extractcmd = substitute ( extractcmd , "-" , "-I lz4 -" , "" ) 736call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 737else 738call system ( "lz4 --decompress --stdout -- " . shellescape ( tarball ) . " | " . extractcmd . " - " . g: tar_secure . shellescape ( fname )) 739endif 740if v: shell_error != 0 741call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 742else 743echo "***note*** successfully extracted " . fname 744endif 745 746elseif tarball =~# "\.tar\.lz4$" 747if has ( "linux" ) 748let extractcmd = substitute ( extractcmd , "-" , "-I lz4 -" , "" ) 749call system ( extractcmd . " " . shellescape ( tarball ) . " " . g: tar_secure . shellescape ( fname )) 750else 751call system ( "lz4 --decompress --stdout -- " . shellescape ( tarball ) . " | " . extractcmd . " - " . g: tar_secure . shellescape ( fname )) 752endif 753if v: shell_error != 0 754call s: Msg ( 'tar#Extract' , 'error' , $ "{extractcmd} {tarball} {fname}: failed!") 755else 756echo "***note*** successfully extracted " . fname 757endif 758endif 759 760" restore option 761let & report = repkeep 762endfun 763 764" --------------------------------------------------------------------- 765" s:Rmdir: {{{2 766fun ! s: Rmdir ( fname ) 767call delete ( a: fname , 'rf' ) 768endfun 769 770" s:FileHeader: {{{2 771fun ! s: Header ( fname ) 772let header = readblob ( a: fname , 0 , 6 ) 773" Nvim: see https://github.com/neovim/neovim/pull/34968 774if header [ 0 : 2 ] == 0 z425A68 " bzip2 header 775return "bzip2" 776elseif header [ 0 : 2 ] == 0 z425A33 " bzip3 header 777return "bzip3" 778elseif header == 0 zFD377A58.5A00 " xz header 779return "xz" 780elseif header [ 0 : 3 ] == 0 z28B52FFD " zstd header 781return "zstd" 782elseif header [ 0 : 3 ] == 0 z04224D18 " lz4 header 783return "lz4" 784elseif ( header [ 0 : 1 ] == 0 z1F9D || 785 \ header[0:1] ==0z1F8B || 786 \ header[0:1] ==0z1F9E || 787 \ header[0:1] ==0z1FA0 || 788 \ header[0:1] ==0z1F1E) 789return "gzip" 790endif 791return "unknown" 792endfun 793 794" --------------------------------------------------------------------- 795" s:WinPath: {{{2 796fun ! s: WinPath ( path ) 797if ( ! g: netrw_cygwin || & shell !~ '\%(\<bash\>\|\<zsh\>\)\%(\.exe\)\=$' ) && has ( "win32" ) 798" remove cygdrive prefix, if present 799let path = substitute ( a: path , '/cygdrive/\(.\)' , '\1:' , '' ) 800" remove trailing slash (Win95) 801let path = substitute ( path , '\(\\\|/\)$' , '' , 'g' ) 802" remove escaped spaces 803let path = substitute ( path , '\ ' , ' ' , 'g' ) 804" convert slashes to backslashes 805let path = substitute ( path , '/' , '\' , 'g' ) 806else 807let path = a: path 808endif 809 810return path 811endfun 812 813" ===================================================================== 814" Modelines And Restoration: {{{1 815let & cpo = s: keepcpo 816unlet s: keepcpo 817" vim:ts=8 fdm=marker