annotate scripts/miscellaneous/unpack.m @ 27569:29ccef7e5295

New function "is_same_file". * utils.cc (Fis_same_file): New function that checks if two paths refer to the same file or folder. * system.txi: Add function to manual. * dir.m, run.m, unpack.m, pkg/private/install.m, fntests.m: Use new function instead of "strcmp".
author Markus Mützel <markus.muetzel@gmx.de>
date Sun, 27 Oct 2019 09:59:39 +0100
parents 3bf19af20af8
children b442ec6dda5c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
26376
00f796120a6d maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents: 26142
diff changeset
1 ## Copyright (C) 2006-2019 Bill Denney
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
2 ##
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
4 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24096
diff changeset
5 ## Octave is free software: you can redistribute it and/or modify it
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24096
diff changeset
7 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22511
diff changeset
8 ## (at your option) any later version.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
9 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22511
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22511
diff changeset
13 ## GNU General Public License for more details.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
14 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6546
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 24096
diff changeset
17 ## <https://www.gnu.org/licenses/>.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
18
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20755
diff changeset
20 ## @deftypefn {} {@var{files} =} unpack (@var{file})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20755
diff changeset
21 ## @deftypefnx {} {@var{files} =} unpack (@var{file}, @var{dir})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20755
diff changeset
22 ## @deftypefnx {} {@var{files} =} unpack (@var{file}, @var{dir}, @var{filetype})
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
23 ## Unpack the archive @var{file} based on its extension to the directory
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
24 ## @var{dir}.
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
25 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
26 ## If @var{file} is a list of strings, then each file is unpacked
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19951
diff changeset
27 ## individually. Shell wildcards in the filename such as @samp{*} or
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19951
diff changeset
28 ## @samp{?} are accepted and expanded.
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
29 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
30 ## If @var{dir} is not specified or is empty (@code{[]}), it defaults to the
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
31 ## current directory. If a directory is in the file list, then @var{filetype}
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
32 ## must also be specified.
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
33 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
34 ## The specific archive filetype is inferred from the extension of the file.
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
35 ## The @var{filetype} may also be specified directly using a string which
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
36 ## corresponds to a known extension.
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
37 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
38 ## Valid filetype extensions:
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
39 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
40 ## @table @code
25003
2365c2661b3c doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24662
diff changeset
41 ## @item @nospell{bz}
2365c2661b3c doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24662
diff changeset
42 ## @itemx @nospell{bz2}
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
43 ## bzip archive
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
44 ##
25003
2365c2661b3c doc: Spellcheck documentation ahead of 4.4 release.
Rik <rik@octave.org>
parents: 24662
diff changeset
45 ## @item @nospell{gz}
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
46 ## gzip archive
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
47 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
48 ## @item tar
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
49 ## tar archive
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
50 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
51 ## @item tarbz
19235
cac9d4c49522 unpack.m: Fix Texinfo error in docstring.
Rik <rik@octave.org>
parents: 19234
diff changeset
52 ## @itemx tarbz2
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
53 ## @itemx tbz
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
54 ## @itemx tbz2
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
55 ## tar + bzip archive
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
56 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
57 ## @item targz
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
58 ## @itemx tgz
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
59 ## tar + gzip archive
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
60 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
61 ## @item z
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
62 ## compress archive
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
63 ##
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
64 ## @item zip
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
65 ## zip archive
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
66 ## @end table
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
67 ##
12642
f96b9b9f141b doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents: 12500
diff changeset
68 ## The optional return value is a list of @var{files} unpacked.
19243
91a6f06c5052 Overhaul gzip.m and gunzip.m
Rik <rik@octave.org>
parents: 19235
diff changeset
69 ## @seealso{bunzip2, gunzip, unzip, untar, bzip2, gzip, zip, tar}
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
70 ## @end deftypefn
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
71
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
72 ## Author: Bill Denney <denney@seas.upenn.edu>
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
73
26142
b9d72a2dac8f Accept a cell array of strings to uncompress functions (bug #55102).
Rik <rik@octave.org>
parents: 25948
diff changeset
74 function filelist = unpack (file, dir = [], filetype = "")
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
75
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
76 if (nargin < 1 || nargin > 3)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
77 print_usage ();
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
78 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
79
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
80 if (! ischar (file) && ! iscellstr (file))
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
81 error ("unpack: FILE must be a string or cell array of strings");
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
82 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
83
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
84 ## Convert char arrays to cell strings to simplify further processing
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
85 if (ischar (file))
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
86 file = cellstr (file);
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
87 endif
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
88 if (numel (file) == 1)
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
89 ## FIXME: The code below is not a perfect test for a URL
20748
bb219f1d92db Fix indexing error in cset 69aeef6736df.
Rik <rik@octave.org>
parents: 20746
diff changeset
90 if (isempty (strfind (file{1}, "://")))
20746
69aeef6736df unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents: 20713
diff changeset
91 gfile = glob (file);
69aeef6736df unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents: 20713
diff changeset
92 if (isempty (gfile))
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
93 error ('unpack: FILE "%s" not found', file{1});
20746
69aeef6736df unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents: 20713
diff changeset
94 else
69aeef6736df unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents: 20713
diff changeset
95 file = gfile;
69aeef6736df unpack.m: Restore ability for input to be a URL (bug #46504).
Rik <rik@octave.org>
parents: 20713
diff changeset
96 endif
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
97 endif
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
98 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
99
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
100 ## Recursively unpack cellstr arrays one file at a time
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
101 if (numel (file) > 1)
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
102 files = {};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
103 for i = 1:numel (file)
26142
b9d72a2dac8f Accept a cell array of strings to uncompress functions (bug #55102).
Rik <rik@octave.org>
parents: 25948
diff changeset
104 if (! isempty (dir))
b9d72a2dac8f Accept a cell array of strings to uncompress functions (bug #55102).
Rik <rik@octave.org>
parents: 25948
diff changeset
105 tmpfiles = unpack (file{i}, dir);
b9d72a2dac8f Accept a cell array of strings to uncompress functions (bug #55102).
Rik <rik@octave.org>
parents: 25948
diff changeset
106 else
b9d72a2dac8f Accept a cell array of strings to uncompress functions (bug #55102).
Rik <rik@octave.org>
parents: 25948
diff changeset
107 tmpfiles = unpack (file{i}, fileparts (file{i}));
b9d72a2dac8f Accept a cell array of strings to uncompress functions (bug #55102).
Rik <rik@octave.org>
parents: 25948
diff changeset
108 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
109 files = {files{:} tmpfiles{:}};
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
110 endfor
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
111
8352
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
112 ## Return output if requested.
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
113 if (nargout > 0)
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
114 filelist = files;
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
115 endif
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
116 return;
8352
33337f1aca75 fix bug in cell string handling of unpack function
Thorsten Meyer <thorsten.meyier@gmx.de>
parents: 8297
diff changeset
117
19233
0ded8964c13a Added wildcard * support to archive-related routines (bug #43211).
Massimiliano Fasi <massimiliano.fasi@gmail.com>
parents: 18857
diff changeset
118 else
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
119 file = file{1};
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
120 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
121
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
122 if (nargin == 3 && (! ischar (filetype) || ! isrow (filetype)))
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
123 error ("unpack: FILETYPE must be a string");
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
124 endif
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
125
25781
e04c56bbbace isdir.m: Make m-file a legacy function (bug #54489)
Rik <rik@octave.org>
parents: 25054
diff changeset
126 if (isfolder (file))
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
127 if (isempty (filetype))
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
128 error ("unpack: FILETYPE must be given for a directory");
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
129 elseif (! strcmpi (filetype, "gz"))
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
130 error ('unpack: FILETYPE must be "gz" for a directory');
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
131 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
132 ext = ".gz";
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
133 else
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
134 [pathstr, name, ext] = fileparts (file);
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
135
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
136 if (nargin == 3 && ! strcmpi (ext, filetype))
25948
700b3f415ebe maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 25893
diff changeset
137 ## override extension with given filetype
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
138 if (isempty (ext))
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
139 ext = filetype;
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
140 else
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
141 ext = regexprep (ext, '(\.?)\S*$', ['$1' filetype]);
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
142 endif
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
143 endif
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
144
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
145 ## Check to see if it's .tar.gz, .tar.Z, etc.
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
146 if (any (strcmpi ({".gz" ".Z" ".bz2" ".bz"}, ext)))
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
147 [~, tmpname, tmpext] = fileparts (name);
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
148 if (strcmpi (tmpext, ".tar"))
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
149 name = tmpname;
16994
333243133364 Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents: 16724
diff changeset
150 ext = [tmpext ext];
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
151 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
152 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
153
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
154 ## If the file is a URL, download it and then work with that file.
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
155 if (! isempty (strfind (file, "://")))
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
156 ## FIXME: The above code is not a perfect test for a URL
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
157 urlfile = file;
20755
46b15c018fa5 unpack.m: Fix handling of gzip archives expanded into /tmp (bug #40274).
Rik <rik@octave.org>
parents: 20748
diff changeset
158 tmpfile = fullfile (tempdir (), [name ext]);
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
159 [file, success, msg] = urlwrite (urlfile, tmpfile);
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
160 if (! success)
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
161 error ('unpack: could not fetch "%s": %s', urlfile, msg);
12499
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
162 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
163 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
164
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
165 endif
a3019189ac51 Improve file archiving functions (gzip, bzip2, zip, unpack)
Rik <octave@nomad.inbox5.com>
parents: 11588
diff changeset
166
19579
0cc52d752f99 unpack.m: replace canonicalize_file_name by make_absolute_filename (bug #43979)
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19278
diff changeset
167 file = make_absolute_filename (file);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
168
20291
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
169 if (isempty (dir))
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
170 dir = ".";
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
171 else
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
172 dir = tilde_expand (dir);
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
173 endif
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
174
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
175 ## Instructions on what to do for any extension.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
176 ##
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
177 ## The field names are the file extension without periods.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
178 ## The first cell is what is executed to unpack an archive verbosely.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
179 ## The second cell is what is executed to unpack an archive quietly.
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
180 ## The third cell is the function to execute on output to get the files list.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
181 ## The fourth cell indicates if the files may need to be manually moved
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
182 ## (i.e., tar and unzip decompress into the current directory while
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
183 ## bzip2 and gzip decompress the file at its location).
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
184 persistent commandlist;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
185 if (isempty (commandlist))
20291
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
186 commandlist.gz = {'gzip -d -v -f -r "%s"', ...
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
187 'gzip -d -f -r "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
188 @__parse_gzip__, true};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
189 commandlist.z = commandlist.gz;
20291
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
190 commandlist.bz2 = {'bzip2 -d -v -f "%s"', ...
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
191 'bzip2 -d -f "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
192 @__parse_bzip2__, true};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
193 commandlist.bz = commandlist.bz2;
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
194 commandlist.tar = {'tar xvf "%s"', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
195 'tar xf "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
196 @__parse_tar__, false};
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
197 commandlist.targz = {'gzip -d -c "%s" | tar xvf -', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
198 'gzip -d -c "%s" | tar xf -', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
199 @__parse_tar__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
200 commandlist.tgz = commandlist.targz;
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
201 commandlist.tarbz2 = {'bzip2 -d -c "%s" | tar xvf -', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
202 'bzip2 -d -c "%s" | tar xf -', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
203 @__parse_tar__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
204 commandlist.tarbz = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
205 commandlist.tbz2 = commandlist.tarbz2;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
206 commandlist.tbz = commandlist.tarbz2;
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
207 commandlist.zip = {'unzip -n "%s"', ...
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
208 'unzip -nq "%s"', ...
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
209 @__parse_zip__, false};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
210 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
211
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
212 ## Unzip doesn't actually care about the extension
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
213 if (strcmpi (filetype, "zip"))
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
214 nodotext = "zip";
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
215 else
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
216 nodotext = ext(ext != '.');
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
217 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
218
19951
8c20fb6caa16 Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19927
diff changeset
219 if (ispc && strcmp (nodotext, "tar"))
8c20fb6caa16 Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19927
diff changeset
220 ## Change file pathname into a mingw style acceptable for tar
8c20fb6caa16 Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19927
diff changeset
221 file = __w2mpth__ (file);
8c20fb6caa16 Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19927
diff changeset
222 endif
8c20fb6caa16 Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19927
diff changeset
223
27569
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
224 ## Create the output directory if necessary.
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
225 s = stat (dir);
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
226 if (isempty (s))
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
227 [status, msg] = mkdir (dir);
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
228 if (! status)
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
229 error ("unpack: mkdir failed to create %s: %s", dir, msg);
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
230 endif
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
231 elseif (! S_ISDIR (s.mode))
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
232 error ("unpack: %s: not a directory", dir);
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
233 endif
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
234
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
235 if (isfield (commandlist, tolower (nodotext)))
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
236 [commandv, commandq, parsefcn, move] = deal (commandlist.(nodotext){:});
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
237 origdir = pwd ();
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
238 if (move)
19597
db92e7e28e1f strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 19579
diff changeset
239 startdir = fileparts (file);
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
240 else
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
241 startdir = origdir;
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
242 endif
19951
8c20fb6caa16 Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19927
diff changeset
243 cstartdir = make_absolute_filename (startdir);
8c20fb6caa16 Fix XFAIL with tar and unpack tests (bug #44007, bug #43979).
Philip Nienhuis <prnienhuis@users.sf.net>
parents: 19927
diff changeset
244 cenddir = make_absolute_filename (dir);
20291
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
245 if (cenddir(end) == filesep)
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
246 cenddir(end) = [];
2fc43288a6c4 unpack.m: Stop hang when unpacking on to existing filename (bug #45331).
Rik <rik@octave.org>
parents: 20164
diff changeset
247 endif
27569
29ccef7e5295 New function "is_same_file".
Markus Mützel <markus.muetzel@gmx.de>
parents: 27331
diff changeset
248 needmove = move && ! is_same_file (cstartdir, cenddir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
249 if (nargout > 0 || needmove)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
250 command = commandv;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
251 else
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
252 command = commandq;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
253 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
254 else
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
255 warning ("unpack: unrecognized FILETYPE <%s>", nodotext);
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
256 filelist = {};
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
257 return;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
258 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
259
24096
8f3db1ac7877 tar, untar, unpack: ensure TAR_OPTIONS doesn't affect behavior (bug #52095)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
260 ## Save and restore the TAR_OPTIONS environment variable used by GNU tar.
8f3db1ac7877 tar, untar, unpack: ensure TAR_OPTIONS doesn't affect behavior (bug #52095)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
261 tar_options_env = getenv ("TAR_OPTIONS");
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
262 unwind_protect
24096
8f3db1ac7877 tar, untar, unpack: ensure TAR_OPTIONS doesn't affect behavior (bug #52095)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
263 unsetenv ("TAR_OPTIONS");
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
264 cd (dir);
16994
333243133364 Use matrix concatenation for strings, rather than cstrcat(), for clarity and performance.
Rik <rik@octave.org>
parents: 16724
diff changeset
265 [status, output] = system (sprintf ([command " 2>&1"], file));
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
266 unwind_protect_cleanup
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
267 cd (origdir);
24096
8f3db1ac7877 tar, untar, unpack: ensure TAR_OPTIONS doesn't affect behavior (bug #52095)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
268 if (! isempty (tar_options_env))
8f3db1ac7877 tar, untar, unpack: ensure TAR_OPTIONS doesn't affect behavior (bug #52095)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
269 setenv ("TAR_OPTIONS", tar_options_env);
8f3db1ac7877 tar, untar, unpack: ensure TAR_OPTIONS doesn't affect behavior (bug #52095)
Mike Miller <mtmiller@octave.org>
parents: 23220
diff changeset
270 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
271 end_unwind_protect
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
272
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
273 if (status)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
274 error ("unpack: unarchiving program exited with status: %d\n%s",
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 9170
diff changeset
275 status, output);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
276 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
277
6084
38114c34b44a [project @ 2006-10-25 03:52:48 by jwe]
jwe
parents: 6083
diff changeset
278 if (nargout > 0 || needmove)
19927
0cc365dada52 Allow tar, unpack, and zip to run on Windows with CR/NL EOL.
Rik <rik@octave.org>
parents: 19697
diff changeset
279 ## Trim the last CR or NL if needed.
0cc365dada52 Allow tar, unpack, and zip to run on Windows with CR/NL EOL.
Rik <rik@octave.org>
parents: 19697
diff changeset
280 files = parsefcn (ostrsplit (output, "\r\n", true))';
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
281
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
282 ## Move files if necessary.
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
283 if (needmove)
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
284 [st, msg] = movefile (files, cenddir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
285 if (! st)
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
286 error ('unpack: unable to move files to "%s": %s', dir, msg);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
287 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
288
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
289 ## Fix the names of the files since they were moved.
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
290 files = strrep (files, cstartdir, cenddir);
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
291 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
292
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
293 ## Return output if requested.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
294 if (nargout > 0)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
295 filelist = files;
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
296 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
297 endif
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
298
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
299 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
300
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
301 function files = __parse_zip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
302 ## Parse the output from zip and unzip.
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
303
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
304 ## Skip first line which is Archive header.
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
305 files = char (output(2:end));
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
306 ## Trim constant width prefix and return cell array.
21385
89fa0694aa2e Fix check-missing-semicolon QA target (bug #47277).
Rik <rik@octave.org>
parents: 20852
diff changeset
307 files = cellstr (files(:,14:end));
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
308 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
309
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
310 function output = __parse_tar__ (output)
27331
3bf19af20af8 tar, untar, unpack: Add support for BSD tar (bug #53695)
Andrew Janke <andrew@apjanke.net>
parents: 26376
diff changeset
311 ## BSD tar emits file actions in the first 2 columns
3bf19af20af8 tar, untar, unpack: Add support for BSD tar (bug #53695)
Andrew Janke <andrew@apjanke.net>
parents: 26376
diff changeset
312
3bf19af20af8 tar, untar, unpack: Add support for BSD tar (bug #53695)
Andrew Janke <andrew@apjanke.net>
parents: 26376
diff changeset
313 if (tar_is_bsd ())
3bf19af20af8 tar, untar, unpack: Add support for BSD tar (bug #53695)
Andrew Janke <andrew@apjanke.net>
parents: 26376
diff changeset
314 output = cellfun (@(x) x(3:end), output, 'UniformOutput', false);
3bf19af20af8 tar, untar, unpack: Add support for BSD tar (bug #53695)
Andrew Janke <andrew@apjanke.net>
parents: 26376
diff changeset
315 endif
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
316 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
317
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
318 function files = __parse_gzip__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
319 ## Parse the output from gzip and gunzip returning the files
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 21571
diff changeset
320 ## compressed (or decompressed).
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
321
21571
feac06371be1 unpack.m: Use more specific regexp to prevent accidental hits.
Rik <rik@octave.org>
parents: 21385
diff changeset
322 files = regexprep (output, '^.+ -- replaced with (.*)$', '$1');
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
323 endfunction
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
324
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
325 function files = __parse_bzip2__ (output)
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
326 ## Parse the output from bzip2 and bunzip2 returning the files
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 21571
diff changeset
327 ## compressed (or decompressed).
6082
588721ac2140 [project @ 2006-10-25 03:46:17 by jwe]
jwe
parents:
diff changeset
328
20713
2469d78a1d8b Consistently use 'filename' rather than 'file name' throughout code base.
Rik <rik@octave.org>
parents: 20291
diff changeset
329 ## Strip leading blanks and .bz2 extension from filename
17000
45165d6c4738 unpack.m: Allow zip files to have any suffix (bug #39148)
Rik <rik@octave.org>
parents: 16994
diff changeset
330 files = regexprep (output, '^\s+(.*)\.bz2: .*', '$1');
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
331 endfunction
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
332
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
333
22270
2c7acd39e9b7 Conditionally disable tests that rely on optional features
Mike Miller <mtmiller@octave.org>
parents: 21758
diff changeset
334 %!testif HAVE_ZLIB
24662
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
335 %! envvar = {"TMPDIR", "TMP"};
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
336 %! envdir = cellfun (@(x) getenv (x), envvar, "uniformoutput", false);
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
337 %! unwind_protect
24662
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
338 %! cellfun (@(x) unsetenv (x), envvar);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
339 %! ## Create temporary directory and file for packing and unpacking
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
340 %! dirname = tempname ();
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
341 %! assert (mkdir (dirname));
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
342 %! filename = tempname ();
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
343 %! fid = fopen (filename, "wt");
19259
f8552bd35180 unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents: 19243
diff changeset
344 %! assert (fid >= 0);
24662
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
345 %! fprintf (fid, "Hello World\n");
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
346 %! fprintf (fid, "123 456 789\n");
19259
f8552bd35180 unpack.m: Rewrite BIST test to not produce warning on stderr.
Rik <rik@octave.org>
parents: 19243
diff changeset
347 %! fclose (fid);
24662
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
348 %!
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
349 %! unwind_protect
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
350 %! copyfile (filename, [filename ".orig"]);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
351 %! gzip (filename, dirname);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
352 %! [~, f] = fileparts (filename);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
353 %! filelist = unpack (fullfile (dirname, [f ".gz"]), tempdir);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
354 %! assert (filelist{1}, filename);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
355 %! fid = fopen ([filename ".orig"], "rb");
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
356 %! assert (fid >= 0);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
357 %! orig_data = fread (fid);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
358 %! fclose (fid);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
359 %! fid = fopen (filename, "rb");
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
360 %! assert (fid >= 0);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
361 %! new_data = fread (fid);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
362 %! fclose (fid);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
363 %! if (orig_data != new_data)
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
364 %! error ("unpack: Unpacked file does not equal original");
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
365 %! endif
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
366 %! unwind_protect_cleanup
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
367 %! unlink (filename);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
368 %! unlink ([filename ".orig"]);
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
369 %! confirm_recursive_rmdir (false, "local");
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
370 %! rmdir (dirname, "s");
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
371 %! end_unwind_protect
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
372 %! unwind_protect_cleanup
24662
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
373 %! ## Restore environment variables TMPDIR, TMP
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
374 %! for i = 1:numel (envvar)
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
375 %! if (isempty (envdir{i}))
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
376 %! unsetenv (envvar{i});
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
377 %! else
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
378 %! setenv (envvar{i}, envdir{i});
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
379 %! endif
276eca875721 Clear environment variables affecting tmpdir before running BIST tests (bug #52627)
Rik <rik@octave.org>
parents: 24534
diff changeset
380 %! endfor
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
381 %! end_unwind_protect
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
382
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
383 ## Test input validation
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
384 %!error unpack ()
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
385 %!error unpack (1,2,3,4)
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
386 %!error <FILE must be a string or cell array of strings> unpack (1)
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22760
diff changeset
387 %!error <FILE "_%NOT_A_FILENAME%_" not found> unpack ("_%NOT_A_FILENAME%_")
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22760
diff changeset
388 %!error <FILE "_%NOT_A_FILENAME%_" not found> unpack ({"_%NOT_A_FILENAME%_"})
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22760
diff changeset
389 %!error <FILE "_%NOT_A_FILENAME%_" not found> unpack ({"_%NOT_A_FILENAME%_", "2nd_filename"})
25893
96c69234ac98 unpack.m: Fix failing BIST test on Windows (bug #54719).
Rik <rik@octave.org>
parents: 25885
diff changeset
390 %!error <FILETYPE must be a string>
96c69234ac98 unpack.m: Fix failing BIST test on Windows (bug #54719).
Rik <rik@octave.org>
parents: 25885
diff changeset
391 %! if (isunix || ismac)
96c69234ac98 unpack.m: Fix failing BIST test on Windows (bug #54719).
Rik <rik@octave.org>
parents: 25885
diff changeset
392 %! unpack ("/", [], 1)
96c69234ac98 unpack.m: Fix failing BIST test on Windows (bug #54719).
Rik <rik@octave.org>
parents: 25885
diff changeset
393 %! else
96c69234ac98 unpack.m: Fix failing BIST test on Windows (bug #54719).
Rik <rik@octave.org>
parents: 25885
diff changeset
394 %! unpack ('C:\', [], 1)
96c69234ac98 unpack.m: Fix failing BIST test on Windows (bug #54719).
Rik <rik@octave.org>
parents: 25885
diff changeset
395 %! endif
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
396 %!error <FILETYPE must be given for a directory>
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
397 %! if (isunix || ismac)
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
398 %! unpack ("/");
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
399 %! else
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
400 %! unpack ('C:\');
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
401 %! endif
25885
75ed70a1c108 Overhaul unpack and associated functions (bug #54567).
Rik <rik@octave.org>
parents: 25781
diff changeset
402 %!error <FILETYPE must be "gz" for a directory>
19234
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
403 %! if (isunix || ismac)
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
404 %! unpack ("/", [], "foobar");
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
405 %! else
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
406 %! unpack ('C:\', [], "foobar");
6ebc2ca01c76 unpack.m: Overhaul function.
Rik <rik@octave.org>
parents: 19233
diff changeset
407 %! endif