From ac2a237ebc68222cb9dc42903bf52e42a94b837c Mon Sep 17 00:00:00 2001 From: vqn Date: Sat, 5 Oct 2024 17:15:04 +0200 Subject: [PATCH] fix unzip command and properly clean up temp files If unzip is compiled with -DWILD_STOP_AT_DIR, the * wildcard does not match the dir separator, so subdirectories are not extracted. --- src/util/zip.sml | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/util/zip.sml b/src/util/zip.sml index c654818..2a54b42 100644 --- a/src/util/zip.sml +++ b/src/util/zip.sml @@ -14,7 +14,7 @@ fun fromFile (s:string) : t = {zipfile=s,deleted=ref false} fun download (url:string) : t = - let val localzip = OS.FileSys.tmpName() ^ ".zip" + let val localzip = OS.FileSys.tmpName() val (status,out,err) = System.command ("curl -L -o " ^ localzip ^ " " ^ url) in if OS.Process.isSuccess status then {zipfile=localzip,deleted=ref false} @@ -37,7 +37,7 @@ fun extractSubDir {log: string -> unit} ({zipfile,deleted}:t) {path:string,targe val () = System.createDirectoryIfMissing true target fun cmds zipfile path target : {zipcmd:string,mvcmd:string,tmpdir:string} = let val tmpdir = target ^ "_tmp~" - in {zipcmd = "unzip " ^ zipfile ^ " '" ^ path ^ "/*' -d " ^ tmpdir, + in {zipcmd = "unzip " ^ zipfile ^ " '" ^ path ^ "/**' -d " ^ tmpdir, mvcmd = "mv " ^ tmpdir ^ "/" ^ path ^ "/* " ^ target ^ "/", tmpdir = tmpdir} end @@ -49,16 +49,21 @@ fun extractSubDir {log: string -> unit} ({zipfile,deleted}:t) {path:string,targe val () = log ("cmd: " ^ zipcmd) val (status,out,err) = System.command zipcmd + + val () = log ("removing " ^ zipfile) + val () = System.removePathForcibly zipfile + val () = if OS.Process.isSuccess status then () else raise Fail ("failed to extract " ^ zipfile ^ ": " ^ err) val () = log ("cmd: " ^ mvcmd) val (status,out,err) = System.command mvcmd - val () = if OS.Process.isSuccess status then () - else raise Fail ("failed to move tmpdir into target: " ^ err) val () = log ("removing " ^ tmpdir) val () = System.removePathForcibly tmpdir + + val () = if OS.Process.isSuccess status then () + else raise Fail ("failed to move tmpdir into target: " ^ err) in () end