# HG changeset patch
# User Markus Mützel <markus.muetzel@gmx.de>
# Date 1505675057 -7200
#      Sun Sep 17 21:04:17 2017 +0200
# Node ID 66b5b97039f00033eba20aecd9d382c2ee5aaec1
# Parent  7f271fa43ec9ba4b1a9e12fad8eb066de7f63adc
Unzipped content instead of actual xlsx file in destination path (bug #51203).

__OCT_spsh_open__.m: Call InfoZip directly with absolute folders instead
of unpack.

__OCT_spsh_close__.m: Zip and move/rename in two steps.

odsclose.m, xlsclose.m: Pass function name to __OCT_spsh_close__.

diff -r 7f271fa43ec9 -r 66b5b97039f0 inst/odsclose.m
--- a/inst/odsclose.m	Fri Jul 28 20:34:54 2017 +0200
+++ b/inst/odsclose.m	Sun Sep 17 21:04:17 2017 +0200
@@ -97,7 +97,7 @@
 
   elseif (strcmp (ods.xtype, "OCT"))
     ## Native Octave
-    ods = __OCT_spsh_close__ (ods);
+    ods = __OCT_spsh_close__ (ods, "odsclose");
 
   ##elseif ---- < Other interfaces here >
 
diff -r 7f271fa43ec9 -r 66b5b97039f0 inst/private/__OCT_spsh_close__.m
--- a/inst/private/__OCT_spsh_close__.m	Fri Jul 28 20:34:54 2017 +0200
+++ b/inst/private/__OCT_spsh_close__.m	Sun Sep 17 21:04:17 2017 +0200
@@ -24,7 +24,7 @@
 ## Author: Philip Nenhuis <prnienhuis at users.sf.net>
 ## Created: 2013-09-09
 
-function [xls] = __OCT_spsh_close__ (xls)
+function [xls] = __OCT_spsh_close__ (xls, fcn)
 
   if (isfield (xls, "nfilename"))
     xls.filename = xls.nfilename;
@@ -47,14 +47,28 @@
             pause (0.05);
       ## Zip tmp directory into .ods or .xlsx and copy it over original file
       try
-        [stts, op] = system (sprintf ('zip -q -r "%s" *.* .', filename));
-        pause (0.20);
+        if exist (fullfile (xls.workbook, [fname ".zip"]), "file")
+          delete (fullfile (xls.workbook, [fname ".zip"]));
+        endif
+        [stts, op] = system (sprintf ('zip -q -r "%s" *', [fname ".zip"]));
+
         if (stts == 0)
+          ## Move file from temp folder and rename
+          [mvst, msg] = movefile (["." filesep fname ".zip"], filename, "f");
+          if ~mvst
+            error ('%s: Moving file "%s" to "%s" failed with message: "%s"', ...
+                   fcn, fullfile (xls.workbook, [fname ".zip"]), filename, msg);
+          endif
           xls.changed = 0;
+        else
+          error ("%s: Zipping xlsx-file failed with error %d\nOutput:\n%s\n", ...
+                 fcn, stts, op);
         endif
-      catch
-        printf ("odsclose: could not zip files in %s to %s\n", ...
-                xls.workbook, filename);
+      catch err
+        printf ("%s: could not zip files in %s to %s\n", ...
+                fcn, xls.workbook, filename);
+        warning (err.message)
+        warning (err.stack)
       end_try_catch;
     endif
 
@@ -72,8 +86,8 @@
       status = 1;
     end_try_catch
     if (status)
-      printf ("odsclose: could not gzip gnumeric contents in %s to %s\n", ...
-              xls.workbook, filename);
+      printf ("%s: could not gzip gnumeric contents in %s to %s\n", ...
+              fcn, xls.workbook, filename);
     endif
   endif
 
diff -r 7f271fa43ec9 -r 66b5b97039f0 inst/private/__OCT_spsh_open__.m
--- a/inst/private/__OCT_spsh_open__.m	Fri Jul 28 20:34:54 2017 +0200
+++ b/inst/private/__OCT_spsh_open__.m	Sun Sep 17 21:04:17 2017 +0200
@@ -61,9 +61,11 @@
   else
     ## xlsx and ods are zipped
     try
-      unpack (templ, tmpdir, "unzip");
-      ## Allow unpack dust to settle down (lazy write file I/O)
-      pause (0.2);
+      ## FIXME: Use "unpack" instead of system ("unzip...") when it is fixed
+      [stts, op] = system (sprintf ('unzip -o "%s" -d %s', templ, tmpdir));
+      if (stts != 0)
+        error ("UnZip failed with error %d\nOutput:\n%s\n", stts, op);
+      endif
     catch err
       printf ("file %s couldn't be unpacked. Is it the proper file format?\n", filename);
       warning (err.message)
@@ -108,7 +110,7 @@
     xls.sheets.sh_names = cell2mat (regexp (sheets, '<sheet name="(.*?)"', "tokens"));
     xls.sheets.rid = str2double (cell2mat (regexp (sheets, ' r:id="rId(\d+)"', "tokens")));
     xls.sheets.sheetid = str2double (cell2mat (regexp (sheets, ' sheetId="(\d+)"', "tokens")));
- 
+
     ## read worksheet.xml.rels
     fid = fopen (sprintf ("%s/xl/_rels/workbook.xml.rels", tmpdir));
     if (fid > 0)
diff -r 7f271fa43ec9 -r 66b5b97039f0 inst/xlsclose.m
--- a/inst/xlsclose.m	Fri Jul 28 20:34:54 2017 +0200
+++ b/inst/xlsclose.m	Sun Sep 17 21:04:17 2017 +0200
@@ -118,7 +118,7 @@
     xls = __UNO_spsh_close__ (xls, force);
 
   elseif (strcmp (xls.xtype, "OCT"))
-    xls = __OCT_spsh_close__ (xls);
+    xls = __OCT_spsh_close__ (xls, "xlsclose");
 
   ## elseif   <other interfaces here>
 
