Mercurial > octave-antonio
annotate scripts/miscellaneous/tar.m @ 19927:0cc365dada52
Allow tar, unpack, and zip to run on Windows with CR/NL EOL.
* tar.m: Don't check for "\n" newline. Use ostrsplit with "\r\n" to split on
any EOL char. Use unlink in BIST test, rather than delete, because it will not
generate an error.
* unpack.m: Don't check for "\n" newline. Use ostrsplit with "\r\n" to split on
any EOL char.
* zip.m: Don't check for "\n" newline. Use ostrsplit with "\r\n" to split on
any EOL char. Use unlink in BIST test, rather than delete, because it will not
generate an error.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 10 Mar 2015 21:23:54 -0700 |
parents | ee7159d9a330 |
children | 8c20fb6caa16 |
rev | line source |
---|---|
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19597
diff
changeset
|
1 ## Copyright (C) 2005-2015 Søren Hauberg |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2 ## |
6049 | 3 ## This file is part of Octave. |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6049 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
5807 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5807 | 18 |
19 ## -*- texinfo -*- | |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
20 ## @deftypefn {Function File} {@var{filelist} =} tar (@var{tarfile}, @var{files}) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{filelist} =} tar (@var{tarfile}, @var{files}, @var{rootdir}) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
22 ## Pack the list of files and directories specified in @var{files} into the |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
23 ## TAR archive @var{tarfile}. |
5808 | 24 ## |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
25 ## @var{files} is a character array or cell array of strings. Shell |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
26 ## wildcards in the filename such as @samp{*} or @samp{?} are accepted and |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
27 ## expanded. Directories are recursively traversed and all files are added to |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
28 ## the archive. |
5807 | 29 ## |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
30 ## If @var{rootdir} is defined then any files without absolute pathnames are |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
31 ## located relative to @var{rootdir} rather than the current directory. |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 ## |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## The optional output @var{filelist} is a list of the files that were included |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
34 ## in the archive. |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## @seealso{untar, unpack, bzip2, gzip, zip} |
5807 | 36 ## @end deftypefn |
37 | |
17243
f4c8c66faf34
maint: Update source file encodings to UTF-8 and fix character errors
Mike Miller <mtmiller@ieee.org>
parents:
16724
diff
changeset
|
38 ## Author: Søren Hauberg <hauberg@gmail.com> |
5807 | 39 |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 function filelist = tar (tarfile, files, rootdir = ".") |
5808 | 41 |
12500
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 if (nargin < 2 || nargin > 3) |
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
43 print_usage (); |
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
44 endif |
5807 | 45 |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
46 if (! ischar (tarfile)) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
47 error ("tar: TARFILE must be a string"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
48 elseif (ischar (files)) |
12500
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 files = cellstr (files); |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
50 elseif (! iscellstr (files)) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 error ("tar: FILES must be a character array or cellstr"); |
12500
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
52 endif |
5807 | 53 |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
54 rootdir = tilde_expand (rootdir); |
5808 | 55 |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
56 tarfile = make_absolute_filename (tarfile); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19535
diff
changeset
|
57 |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
58 cmd = sprintf ("tar cvf %s -C %s %s", |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
59 tarfile, rootdir, sprintf (" %s", files{:})); |
12500
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
60 |
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 [status, output] = system (cmd); |
5807 | 62 |
12500
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 if (status) |
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 error ("tar: tar exited with status = %d", status); |
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
65 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
66 |
12500
8f2056646dba
Improve docstrings for archive functions (gzip, bzip2, etc.)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
67 if (nargout > 0) |
19927
0cc365dada52
Allow tar, unpack, and zip to run on Windows with CR/NL EOL.
Rik <rik@octave.org>
parents:
19925
diff
changeset
|
68 filelist = ostrsplit (output, "\r\n", true); |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 filelist = filelist'; |
5808 | 70 endif |
71 | |
5807 | 72 endfunction |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17243
diff
changeset
|
73 |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
74 |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
75 %!xtest |
19925
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
76 %! ## test tar together with untar |
19535
827cc33ae05c
avoid warning message in test for tar function
John W. Eaton <jwe@octave.org>
parents:
19258
diff
changeset
|
77 %! orig_dir = pwd (); |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 %! unwind_protect |
19925
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
79 %! dirname = tarname = outdir = ""; |
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
80 %! dirname = tempname (); |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
81 %! assert (mkdir (dirname)); |
19535
827cc33ae05c
avoid warning message in test for tar function
John W. Eaton <jwe@octave.org>
parents:
19258
diff
changeset
|
82 %! chdir (dirname); |
827cc33ae05c
avoid warning message in test for tar function
John W. Eaton <jwe@octave.org>
parents:
19258
diff
changeset
|
83 %! dirname2 = "dir2"; |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
84 %! assert (mkdir (dirname2)); |
19925
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
85 %! fname1 = "file1"; |
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
86 %! fname2 = fullfile (dirname2, "file2"); |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
87 %! fid = fopen (fname1, "wt"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
88 %! assert (fid >= 0); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
89 %! fdisp (fid, "Hello World"); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19535
diff
changeset
|
90 %! fclose (fid); |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 %! fid = fopen (fname2, "wt"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
92 %! assert (fid >= 0); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
93 %! fdisp (fid, "Goodbye World"); |
19597
db92e7e28e1f
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19535
diff
changeset
|
94 %! fclose (fid); |
19925
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
95 %! tarname = [tempname() ".tar"]; |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 %! filelist = tar (tarname, {dirname2, fname1}); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 %! if (! strcmp (filelist{3}, fname1)) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
98 %! error ("tar file contents does not match expected file"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 %! endif |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
100 %! if (! exist (tarname, "file")) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
101 %! error ("tar archive file cannot be found!"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
102 %! endif |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
103 %! outdir = tempname; |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 %! untar (tarname, outdir); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 %! fid = fopen (fullfile (outdir, fname1), "rt"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
106 %! assert (fid >= 0); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 %! str = fgetl (fid); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
108 %! fclose (fid); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 %! assert (str, "Hello World"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 %! fid = fopen (fullfile (outdir, fname2), "rt"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 %! assert (fid >= 0); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 %! str = fgetl (fid); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 %! fclose (fid); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 %! assert (str, "Goodbye World"); |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
115 %! unwind_protect_cleanup |
19925
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
116 %! chdir (orig_dir); |
19927
0cc365dada52
Allow tar, unpack, and zip to run on Windows with CR/NL EOL.
Rik <rik@octave.org>
parents:
19925
diff
changeset
|
117 %! unlink (tarname); |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
118 %! confirm_recursive_rmdir (false, "local"); |
19925
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
119 %! if (exist (dirname)) |
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
120 %! rmdir (dirname, "s"); |
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
121 %! endif |
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
122 %! if (exist (outdir)) |
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
123 %! rmdir (outdir, "s"); |
ee7159d9a330
tar.m: Re-code BIST test restore working directory even when test fails (bug #44007).
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
124 %! endif |
19258
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
125 %! end_unwind_protect |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
126 |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 ## Test input validation |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 %!error tar () |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 %!error tar (1) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
130 %!error tar (1,2,3,4) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 %!error <TARFILE must be a string> tar (1, "foobar") |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
132 %!error <FILES must be a character array or cellstr> tar ("foobar", 1) |
a4e993343e93
Overhaul the archive family (bzip2, gzip, zip, tar) of m-files.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
133 |