mput

PURPOSE ^

MPUT Upload file(s) to an SFTP server.

SYNOPSIS ^

function list = mput(h, path)

DESCRIPTION ^

MPUT  Upload file(s) to an SFTP server.

  Syntax:
    MPUT(H, PATH)
    LIST = MPUT(H, PATH)

  Description:
    MPUT(H, PATH) uploads file(s) to the server.
    If the path is a directory, the directory and its contents are uploaded.
    If the path is a file, the file itself is uploaded.
    Otherwise, the path is considered a glob which may contain wildcards 
    ('*'), and only files matching the glob are uploaded, if any.

    LIST = MPUT(H, ...) returns the list of uploaded files.

  Examples:
    % Upload file to remote working directory:
    mput(h, filename)
    % Upload all files and directories in current working directory:
    mput(h, '*')
    % Upoad all hidden files and directories to remote working directory.
    list = mput(h, '.*')

  See also:
    SFTP

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

mput.m

SOURCE CODE ^

0001 function list = mput(h, path)
0002 %MPUT  Upload file(s) to an SFTP server.
0003 %
0004 %  Syntax:
0005 %    MPUT(H, PATH)
0006 %    LIST = MPUT(H, PATH)
0007 %
0008 %  Description:
0009 %    MPUT(H, PATH) uploads file(s) to the server.
0010 %    If the path is a directory, the directory and its contents are uploaded.
0011 %    If the path is a file, the file itself is uploaded.
0012 %    Otherwise, the path is considered a glob which may contain wildcards
0013 %    ('*'), and only files matching the glob are uploaded, if any.
0014 %
0015 %    LIST = MPUT(H, ...) returns the list of uploaded files.
0016 %
0017 %  Examples:
0018 %    % Upload file to remote working directory:
0019 %    mput(h, filename)
0020 %    % Upload all files and directories in current working directory:
0021 %    mput(h, '*')
0022 %    % Upoad all hidden files and directories to remote working directory.
0023 %    list = mput(h, '.*')
0024 %
0025 %  See also:
0026 %    SFTP
0027 %
0028 %  Authors:
0029 %    Joan Pau Beltran  <joanpau.beltran@socib.cat>
0030 
0031 %  Copyright (C) 2014-2016
0032 %  ICTS SOCIB - Servei d'observacio i prediccio costaner de les Illes Balears
0033 %  <http://www.socib.es>
0034 %
0035 %  This program is free software: you can redistribute it and/or modify
0036 %  it under the terms of the GNU General Public License as published by
0037 %  the Free Software Foundation, either version 3 of the License, or
0038 %  (at your option) any later version.
0039 %
0040 %  This program is distributed in the hope that it will be useful,
0041 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
0042 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0043 %  GNU General Public License for more details.
0044 %
0045 %  You should have received a copy of the GNU General Public License
0046 %  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0047 
0048   [status, attrout] = fileattrib(path);
0049   if ~status
0050     error('sftp:mget:FileError', attrout);
0051   end
0052   % FILEATTRIB recurses down directories when passed a glob, use DIR instead.
0053   if isscalar(attrout)
0054     [source, name, ext] = fileparts(attrout.Name);
0055     atts = struct();
0056     atts.name = [name ext];
0057     atts.isdir = attrout.directory;
0058   else
0059     [source, name, ext] = fileparts(path);
0060     if isempty(source)
0061       source = pwd();
0062     end
0063     atts = dir(path);
0064     atts = atts(~(strcmp({atts.name}, '.') | strcmp({atts.name}, '..')));
0065   end
0066     
0067   target = mexsftp('pwd', h.sftp_handle);
0068   if target(end) ~= '/'
0069     target = [target '/'];
0070   end
0071   dflags = [atts.isdir]';
0072   rpaths = {atts.name}';
0073   list = cell(0,1);
0074   while ~isempty(rpaths)
0075     rpath = rpaths{end};
0076     dflag = dflags(end);
0077     lpath = strrep(rpath, '/', filesep());
0078     rpaths(end) = [];
0079     dflags(end) = [];
0080     if dflag
0081       mkdir(h, rpath);
0082       atts = dir(fullfile(source, lpath));
0083       atts = atts(~(strcmp({atts.name}, '.') | strcmp({atts.name}, '..')));
0084       if ~isempty(atts)
0085         dflags(end + (1:numel(atts))) = [atts.isdir]';
0086         rpaths(end + (1:numel(atts))) = strcat(rpath, '/', {atts.name}');
0087       end
0088     else
0089       mexsftp('putfile', h.sftp_handle, ...
0090               fullfile(source, lpath), strcat(target, rpath));
0091     end
0092     list{end+1, 1} = strcat(target, rpath);
0093   end
0094   
0095 end

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