mget

PURPOSE ^

MGET Download file(s) from an SFTP server.

SYNOPSIS ^

function list = mget(h, path, target)

DESCRIPTION ^

MGET  Download file(s) from an SFTP server.

  Syntax:
    MGET(H, PATH)
    MGET(H, PATH, TARGET)
    LIST = MGET(H, ...)

  Description:
    MGET(H, PATH) downloads file(s) from the server to the current directory.
    If the path is a directory, the directory and its contents are downloaded.
    If the path is a file, tha file itself is downloaded.
    Otherwise, the path is considered a glob which may contain wildcards 
    ('?' or '*'), and only files matching the glob are downloaded, if any.

    MGET(H, PATH, TARGET) downloads the file(s) to the given target directory
    instead of the current one.

    LIST = MGET(H, ...) returns the list of downloaded files.

  Examples:
    % Download file from remote working directory to current working directory:
    mget(h, filename)
    % Download file from remote working directory to another directory:
    mget(h, filename, target)
    % Download all files and directories in remote working directory:
    mget(h, '*')
    % Download all hidden files and directories in remote working directory,
    % to a different directory:
    list = mget(h, '.*', stash)

  See also:
    SFTP

  Authors:
    Joan Pau Beltran  <joanpau.beltran@socib.cat>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

mget.m

SOURCE CODE ^

0001 function list = mget(h, path, target)
0002 %MGET  Download file(s) from an SFTP server.
0003 %
0004 %  Syntax:
0005 %    MGET(H, PATH)
0006 %    MGET(H, PATH, TARGET)
0007 %    LIST = MGET(H, ...)
0008 %
0009 %  Description:
0010 %    MGET(H, PATH) downloads file(s) from the server to the current directory.
0011 %    If the path is a directory, the directory and its contents are downloaded.
0012 %    If the path is a file, tha file itself is downloaded.
0013 %    Otherwise, the path is considered a glob which may contain wildcards
0014 %    ('?' or '*'), and only files matching the glob are downloaded, if any.
0015 %
0016 %    MGET(H, PATH, TARGET) downloads the file(s) to the given target directory
0017 %    instead of the current one.
0018 %
0019 %    LIST = MGET(H, ...) returns the list of downloaded files.
0020 %
0021 %  Examples:
0022 %    % Download file from remote working directory to current working directory:
0023 %    mget(h, filename)
0024 %    % Download file from remote working directory to another directory:
0025 %    mget(h, filename, target)
0026 %    % Download all files and directories in remote working directory:
0027 %    mget(h, '*')
0028 %    % Download all hidden files and directories in remote working directory,
0029 %    % to a different directory:
0030 %    list = mget(h, '.*', stash)
0031 %
0032 %  See also:
0033 %    SFTP
0034 %
0035 %  Authors:
0036 %    Joan Pau Beltran  <joanpau.beltran@socib.cat>
0037 
0038 %  Copyright (C) 2014-2016
0039 %  ICTS SOCIB - Servei d'observacio i prediccio costaner de les Illes Balears
0040 %  <http://www.socib.es>
0041 %
0042 %  This program is free software: you can redistribute it and/or modify
0043 %  it under the terms of the GNU General Public License as published by
0044 %  the Free Software Foundation, either version 3 of the License, or
0045 %  (at your option) any later version.
0046 %
0047 %  This program is distributed in the hope that it will be useful,
0048 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
0049 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0050 %  GNU General Public License for more details.
0051 %
0052 %  You should have received a copy of the GNU General Public License
0053 %  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0054 
0055   if (nargin < 3)
0056     target = pwd();
0057   end
0058     
0059   try
0060     atts = mexsftp('lsfile', h.sftp_handle, path);
0061   catch exception
0062     if ~strcmp(exception.identifier, 'sftp:lsfile:ListError')
0063       rethrow(exception);
0064     end
0065     atts = mexsftp('lsglob', h.sftp_handle, path);
0066   end
0067   if isempty(atts)
0068    error('sftp:mget:NotFound', ...
0069          'No such file or directory: %s.', path);
0070   end
0071   
0072   filesep_index = find(path == '/', 1, 'last');
0073   if isempty(filesep_index)
0074     rprefix = '';
0075     lprefix = target;
0076   else
0077     rprefix = path(1:filesep_index);
0078     lprefix = fullfile(target, strrep(rprefix, '/', filesep()));
0079   end
0080   [status, attrout] = fileattrib(lprefix);
0081   if ~status
0082     [success, message] = mkdir(lprefix);
0083     if ~success
0084       error('sftp:mget:DirectoryError', ...
0085             'Could not create directory %s: %s.', lprefix, message);
0086     end
0087   elseif ~attrout.directory
0088     error('sftp:mget:DirectoryError', 'Not a directory: %s.', lprefix);
0089   end
0090   
0091   dflags = [atts.isdir]';
0092   rpaths = strcat(rprefix, {atts.name}');
0093   list = cell(0,1);
0094   while ~isempty(rpaths)
0095     rpath = rpaths{end};
0096     dflag = dflags(end);
0097     lpath = fullfile(target, strrep(rpath, '/', filesep()));
0098     rpaths(end) = [];
0099     dflags(end) = [];
0100     if dflag
0101       [status, attrout] = fileattrib(lpath);
0102       if ~status
0103         [success, message] = mkdir(lpath);
0104         if ~success
0105           error('sftp:mget:DirectoryError', ...
0106                 'Could not create directory %s: %s.', lpath, message);
0107         end
0108       elseif ~attrout.directory
0109         error('sftp:mget:DirectoryError', 'Not a directory: %s.', attrout.Name);
0110       end
0111       atts = mexsftp('lsdir', h.sftp_handle, rpath);
0112       if ~isempty(atts)
0113         dflags(end + (1:numel(atts))) = [atts.isdir]';
0114         rpaths(end + (1:numel(atts))) = strcat(rpath, '/', {atts.name}');
0115       end
0116     else
0117       mexsftp('getfile', h.sftp_handle, rpath, lpath);
0118     end
0119     list{end+1, 1} = lpath;
0120   end
0121   
0122 end

Generated on Fri 06-Oct-2017 10:47:42 by m2html © 2005