comparison scripts/miscellaneous/untar.m @ 5808:a18d85bdff31

[project @ 2006-05-11 03:11:03 by jwe]
author jwe
date Thu, 11 May 2006 03:11:08 +0000
parents 29c4fb42b210
children 8467683311f8
comparison
equal deleted inserted replaced
5807:29c4fb42b210 5808:a18d85bdff31
13 ## You should have received a copy of the GNU General Public License 13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; if not, write to the Free Software 14 ## along with this program; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 16
17 ## -*- texinfo -*- 17 ## -*- texinfo -*-
18 ## @deftypefn {Function File} untar (@var{filename}) 18 ## @deftypefn {Function File} untar (@var{tarfile}, @var{dir})
19 ## @deftypefnx {Function File} untar (@var{filename}, @var{outputdir}) 19 ## Unpack the TAR archive @var{tarfile} to the directory @var{dir}.
20 ## Unpacks the archive @var{filename} using the tar program. 20 ## If @var{dir} is not specified, it defaults to the current directory.
21 ## The resulting files are placed in the directory @var{outputdir}, 21 ## @seealso{tar, gzip, gunzip, zip, unzip}
22 ## which defaults to the current directory.
23 ## The used uncompressing algorithm depends on the extension of
24 ## @var{filename}. If @var{filename} doesn't have an extension a list
25 ## of supported extensions is tried.
26 ## @end deftypefn 22 ## @end deftypefn
27 23
28 ## Author: Søren Hauberg <hauberg at gmail dot com> 24 ## Author: Søren Hauberg <hauberg@gmail.com>
25 ## Adapted-By: jwe
29 26
30 # XXX: how do we support returning the extracted filenames? 27 function files = untar (tarfile, dir)
31 # If strip was sane, we could simply return strip(output),
32 # which is what is currently done. However this is not
33 # Matlab compatible, since Matlab returns a cellstr and
34 # this implementation returns a matrix.
35 function out = untar(filename, outputdir)
36 if (nargin == 0)
37 print_usage("untar");
38 elseif (nargin == 1)
39 outputdir = ".";
40 endif
41
42 ## If filename is a sq_string, convert it to a string
43 filename = sprintf("%s", filename);
44
45 ## XXX: what about "" (i.e. no extension) ?
46 supported_extensions = {"tar", "tar.gz", "tgz", "tar.bz", "tar.bz2", "tbz", "tbz2"};
47 28
48 ## Make sure filename and outputdir are strings 29 if (nargin == 1 || nargin == 2)
49 if (!ischar(filename) || !ischar(outputdir))
50 error("All arguments must be strings.\n");
51 endif
52
53 ## Get extension of filename
54 dots = find(filename == ".");
55 for dot = dots
56 curext = filename(dot+1:end);
57 if (any(strcmp(curext, supported_extensions)))
58 ext = curext;
59 break;
60 endif
61 endfor
62 30
63 ## If no extension was found, iterate over possible extensions 31 if (nargin == 1)
64 ## and try to append them to filename 32 dir = ".";
65 if (!exist("ext", "var"))
66 for i = 1:length(supported_extensions)
67 curext = supported_extensions{i};
68 if (exist([filename "." curext], "file"))
69 filename = [filename "." curext];
70 ext = curext;
71 break;
72 endif
73 endfor
74 endif 33 endif
75 34
76 ## If no usable extension was found give an error 35 if (ischar (tarfile) && ischar (dir))
77 if (!exist("ext", "var")) 36
78 error("No supported extension was found for %s\n", filename); 37 orig_dir = pwd ();
38
39 tarfile = canonicalize_file_name (tarfile);
40
41 s = stat (dir);
42 if (isempty (s))
43 [status, msg] = mkdir (dir);
44 if (! status)
45 error ("untar: mkdir failed to create %s: %s", dir, msg);
46 endif
47 elseif (! S_ISDIR (s.mode))
48 error ("untar: %s: not a directory", dir);
49 endif
50
51 unwind_protect
52 chdir (dir);
53 [status, output] = system (sprintf ("tar -x -v -f %s", tarfile));
54 unwind_protect_cleanup
55 chdir (orig_dir);
56 end_unwind_protect
57
58 if (status == 0)
59 if (nargout > 0)
60 fs = filesep ();
61 if (dir(end) != fs)
62 dir = strcat (dir, fs);
63 endif
64 ## Sadly not reliable if a filename contains a newline
65 ## character!
66 if (output(end) == "\n")
67 output(end) = [];
68 endif
69 files = cellstr (split (output, "\n"));
70 if (! strcmp (dir, "."))
71 nf = length (files);
72 for i = 1:nf
73 files{i} = strcat (dir, files{i});
74 endfor
75 endif
76 files = files';
77 endif
78 else
79 error ("tar: tar exited with status = %s", status);
80 endif
81
82 else
83 error ("untar: expecting arguments to be character strings");
79 endif 84 endif
80
81 ## Determine which flags to use with tar
82 switch (ext)
83 case {"tar"} flag = "";
84 case {"tar.gz", "tgz"} flag = "z";
85 case {"tar.bz", "tar.bz2", "tbz", "tbz2"} flag = "j";
86 endswitch
87 85
88 ## Call tar 86 else
89 [output, status] = system(["tar -" flag "xvf " filename " -C " outputdir]); 87 print_usage ("untar");
90 if (status != 0) 88 endif
91 error("tar returned the following error: %s\n", output); 89
92 endif
93
94 if (nargout > 0)
95 out = split(output, "\n");
96 endif
97 endfunction 90 endfunction