5807
|
1 ## Copyright (C) 2005 S�ren Hauberg |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
6049
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
5807
|
19 |
|
20 ## -*- texinfo -*- |
5808
|
21 ## @deftypefn {Function File} untar (@var{tarfile}, @var{dir}) |
|
22 ## Unpack the TAR archive @var{tarfile} to the directory @var{dir}. |
|
23 ## If @var{dir} is not specified, it defaults to the current directory. |
|
24 ## @seealso{tar, gzip, gunzip, zip, unzip} |
5807
|
25 ## @end deftypefn |
|
26 |
5808
|
27 ## Author: S�ren Hauberg <hauberg@gmail.com> |
|
28 ## Adapted-By: jwe |
5807
|
29 |
6041
|
30 function files = untar (tarfile, directory) |
5807
|
31 |
5808
|
32 if (nargin == 1 || nargin == 2) |
5807
|
33 |
5808
|
34 if (nargin == 1) |
6041
|
35 directory = "."; |
5807
|
36 endif |
|
37 |
6041
|
38 ## The file must exist (and be a file) and the directory must be a |
|
39 ## string. |
|
40 if (exist (tarfile, "file") && ischar (directory)) |
5808
|
41 |
|
42 orig_dir = pwd (); |
|
43 |
|
44 tarfile = canonicalize_file_name (tarfile); |
|
45 |
6041
|
46 s = stat (directory); |
5808
|
47 if (isempty (s)) |
6041
|
48 [status, msg] = mkdir (directory); |
5808
|
49 if (! status) |
6041
|
50 error ("untar: mkdir failed to create %s: %s", directory, msg); |
5808
|
51 endif |
|
52 elseif (! S_ISDIR (s.mode)) |
6041
|
53 error ("untar: %s: not a directory", directory); |
5808
|
54 endif |
|
55 |
|
56 unwind_protect |
6041
|
57 chdir (directory); |
5808
|
58 [status, output] = system (sprintf ("tar -x -v -f %s", tarfile)); |
|
59 unwind_protect_cleanup |
|
60 chdir (orig_dir); |
|
61 end_unwind_protect |
|
62 |
|
63 if (status == 0) |
|
64 if (nargout > 0) |
|
65 fs = filesep (); |
6041
|
66 if (directory(end) != fs) |
|
67 directory = strcat (directory, fs); |
5808
|
68 endif |
|
69 ## Sadly not reliable if a filename contains a newline |
|
70 ## character! |
|
71 if (output(end) == "\n") |
|
72 output(end) = []; |
|
73 endif |
|
74 files = cellstr (split (output, "\n")); |
6041
|
75 if (! strcmp (directory, ".")) |
5808
|
76 nf = length (files); |
|
77 for i = 1:nf |
6041
|
78 files{i} = strcat (directory, files{i}); |
5808
|
79 endfor |
|
80 endif |
|
81 files = files'; |
|
82 endif |
|
83 else |
|
84 error ("tar: tar exited with status = %s", status); |
|
85 endif |
|
86 |
|
87 else |
|
88 error ("untar: expecting arguments to be character strings"); |
5807
|
89 endif |
|
90 |
5808
|
91 else |
|
92 print_usage ("untar"); |
|
93 endif |
|
94 |
5807
|
95 endfunction |