View | Details | Raw Unified | Return to bug 39373
Collapse All | Expand All

(-)ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/AbstractSshMessage.java (+43 lines)
Lines 21-26 Link Here
21
import com.jcraft.jsch.ChannelExec;
21
import com.jcraft.jsch.ChannelExec;
22
import com.jcraft.jsch.JSchException;
22
import com.jcraft.jsch.JSchException;
23
import com.jcraft.jsch.Session;
23
import com.jcraft.jsch.Session;
24
import com.jcraft.jsch.ChannelSftp;
25
import com.jcraft.jsch.SftpATTRS;
26
import com.jcraft.jsch.SftpException;
27
import com.jcraft.jsch.SftpProgressMonitor;
24
28
25
import java.io.IOException;
29
import java.io.IOException;
26
import java.io.OutputStream;
30
import java.io.OutputStream;
Lines 75-80 Link Here
75
    }
79
    }
76
80
77
    /**
81
    /**
82
     * Open an ssh sftp channel.
83
     * @return the channel
84
     * @throws JSchException on error
85
     */
86
    protected ChannelSftp openSftpChannel() throws JSchException {
87
        ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
88
89
        return channel;
90
    }
91
92
    /**
78
     * Send an ack.
93
     * Send an ack.
79
     * @param out the output stream to use
94
     * @param out the output stream to use
80
     * @throws IOException on error
95
     * @throws IOException on error
Lines 213-216 Link Here
213
        return percent;
228
        return percent;
214
    }
229
    }
215
230
231
    private ProgressMonitor monitor=null;
232
233
    ProgressMonitor getProgressMonitor(){
234
	if(monitor==null)monitor=new ProgressMonitor();
235
	return monitor;
236
    }
237
    class ProgressMonitor implements SftpProgressMonitor{
238
	long initFileSize=0;
239
	long totalLength=0;
240
	int percentTransmitted = 0;
241
	public void init(int op, String src, String dest, long max){
242
	    initFileSize=max;
243
	    totalLength=0;
244
	    percentTransmitted = 0;
245
	}
246
	public boolean count(long len){
247
	    totalLength+=len;
248
	    percentTransmitted = trackProgress(initFileSize, 
249
					       totalLength, 
250
					       percentTransmitted);
251
	    return true;
252
	}
253
	public void end(){
254
	}
255
	public long getTotalLength(){
256
	    return totalLength; 
257
	}
258
    };
216
}
259
}
(-)ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/Scp.java (-9 / +43 lines)
Lines 44-49 Link Here
44
    private String toUri;
44
    private String toUri;
45
    private List fileSets = null;
45
    private List fileSets = null;
46
    private boolean isFromRemote, isToRemote;
46
    private boolean isFromRemote, isToRemote;
47
    private boolean isSftp = false;
47
48
48
    /**
49
    /**
49
     * Sets the file to be transferred.  This can either be a remote
50
     * Sets the file to be transferred.  This can either be a remote
Lines 142-147 Link Here
142
    }
143
    }
143
144
144
    /**
145
    /**
146
     * Setting this to true to use sftp protocol.
147
     *
148
     * @param yesOrNo if true sftp protocol will be used.
149
     */
150
    public void setSftp(boolean yesOrNo) {
151
        isSftp=yesOrNo;
152
    }
153
154
    /**
145
     * Adds a FileSet tranfer to remote host.  NOTE: Either
155
     * Adds a FileSet tranfer to remote host.  NOTE: Either
146
     * addFileSet() or setFile() are required.  But, not both.
156
     * addFileSet() or setFile() are required.  But, not both.
147
     *
157
     *
Lines 213-222 Link Here
213
        Session session = null;
223
        Session session = null;
214
        try {
224
        try {
215
            session = openSession();
225
            session = openSession();
216
            ScpFromMessage message =
226
            ScpFromMessage message = null;
217
                new ScpFromMessage(getVerbose(), session, file,
227
	    if(!isSftp){
218
                                   getProject().resolveFile(toPath),
228
		message =
219
                                   fromSshUri.endsWith("*"));
229
		    new ScpFromMessage(getVerbose(), session, file,
230
				       getProject().resolveFile(toPath),
231
				       fromSshUri.endsWith("*"));
232
	    }
233
	    else{
234
		message =
235
		    new ScpFromMessageBySftp(getVerbose(), session, file,
236
					     getProject().resolveFile(toPath),
237
					     fromSshUri.endsWith("*"));
238
	    }
220
            log("Receiving file: " + file);
239
            log("Receiving file: " + file);
221
            message.setLogListener(this);
240
            message.setLogListener(this);
222
            message.execute();
241
            message.execute();
Lines 243-250 Link Here
243
            }
262
            }
244
            if (!list.isEmpty()) {
263
            if (!list.isEmpty()) {
245
                session = openSession();
264
                session = openSession();
246
                ScpToMessage message = new ScpToMessage(getVerbose(), session,
265
		ScpToMessage message = null;
247
                                                        list, file);
266
		if(!isSftp){
267
		    message = new ScpToMessage(getVerbose(), session,
268
					       list, file);
269
		}
270
		else{
271
		    message = new ScpToMessageBySftp(getVerbose(), session,
272
						     list, file);
273
		}
248
                message.setLogListener(this);
274
                message.setLogListener(this);
249
                message.execute();
275
                message.execute();
250
            }
276
            }
Lines 262-270 Link Here
262
        Session session = null;
288
        Session session = null;
263
        try {
289
        try {
264
            session = openSession();
290
            session = openSession();
265
            ScpToMessage message =
291
	    ScpToMessage message = null;
266
                new ScpToMessage(getVerbose(), session,
292
	    if(!isSftp){
267
                                 getProject().resolveFile(fromPath), file);
293
		message =
294
		    new ScpToMessage(getVerbose(), session,
295
				     getProject().resolveFile(fromPath), file);
296
	    }
297
	    else{
298
		message =
299
		    new ScpToMessageBySftp(getVerbose(), session,
300
					   getProject().resolveFile(fromPath), file);
301
	    }
268
            message.setLogListener(this);
302
            message.setLogListener(this);
269
            message.execute();
303
            message.execute();
270
        } finally {
304
        } finally {
(-)ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessage.java (+18 lines)
Lines 41-46 Link Here
41
    private boolean isRecursive = false;
41
    private boolean isRecursive = false;
42
42
43
    /**
43
    /**
44
     * Constructor for ScpFromMessage
45
     * @param session the ssh session to use
46
     */
47
    public ScpFromMessage(Session session) {
48
        super(session);
49
    }
50
51
    /**
52
     * Constructor for ScpFromMessage
53
     * @param verbose if true do verbose logging
54
     * @param session the ssh session to use
55
     * @since Ant 1.6.2
56
     */
57
    public ScpFromMessage(boolean verbose, Session session) {
58
	super(verbose, session);
59
    }
60
61
    /**
44
     * Constructor for ScpFromMessage.
62
     * Constructor for ScpFromMessage.
45
     * @param verbose if true log extra information
63
     * @param verbose if true log extra information
46
     * @param session the Scp session to use
64
     * @param session the Scp session to use
(-)ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpFromMessageBySftp.java (+177 lines)
Line 0 Link Here
1
/*
2
 * Copyright  2003-2005 The Apache Software Foundation
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 *
16
 */
17
18
package org.apache.tools.ant.taskdefs.optional.ssh;
19
20
import java.io.File;
21
import java.io.IOException;
22
import java.io.EOFException;
23
import java.io.InputStream;
24
import java.io.OutputStream;
25
import java.io.FileOutputStream;
26
import java.io.ByteArrayOutputStream;
27
28
import com.jcraft.jsch.JSchException;
29
import com.jcraft.jsch.Session;
30
import com.jcraft.jsch.Channel;
31
import com.jcraft.jsch.ChannelSftp;
32
import com.jcraft.jsch.SftpException;
33
import com.jcraft.jsch.SftpATTRS;
34
import com.jcraft.jsch.SftpProgressMonitor;
35
36
/**
37
 * A helper object representing an scp download.
38
 */
39
public class ScpFromMessageBySftp extends ScpFromMessage {
40
41
    private String remoteFile;
42
    private File localFile;
43
    private boolean isRecursive = false;
44
    private boolean verbose = false;
45
46
47
    /**
48
     * Constructor for ScpFromMessageBySftp.
49
     * @param verbose if true log extra information
50
     * @param session the Scp session to use
51
     * @param aRemoteFile the remote file name
52
     * @param aLocalFile  the local file
53
     * @param recursive   if true use recursion
54
     * @since Ant 1.6.2
55
     */
56
    public ScpFromMessageBySftp(boolean verbose,
57
				Session session,
58
				String aRemoteFile,
59
				File aLocalFile,
60
				boolean recursive) {
61
        super(verbose, session);
62
        this.verbose = verbose;
63
        this.remoteFile = aRemoteFile;
64
        this.localFile = aLocalFile;
65
        this.isRecursive = recursive;
66
    }
67
68
    /**
69
     * Constructor for ScpFromMessageBySftp.
70
     * @param session the Scp session to use
71
     * @param aRemoteFile the remote file name
72
     * @param aLocalFile  the local file
73
     * @param recursive   if true use recursion
74
     */
75
    public ScpFromMessageBySftp(Session session,
76
				String aRemoteFile,
77
				File aLocalFile,
78
				boolean recursive) {
79
        this(false, session, aRemoteFile, aLocalFile, recursive);
80
    }
81
82
    /**
83
     * Carry out the transfer.
84
     * @throws IOException on i/o errors
85
     * @throws JSchException on errors detected by scp
86
     */
87
    public void execute() throws IOException, JSchException {
88
        ChannelSftp channel = openSftpChannel();
89
        try {
90
            channel.connect();
91
	    try {
92
		SftpATTRS attrs=channel.stat(remoteFile);
93
		if(attrs.isDir() && !remoteFile.endsWith("/")){
94
		    remoteFile=remoteFile+"/";
95
		}
96
	    } catch(SftpException ee) {
97
	    }
98
	    getDir(channel, remoteFile, localFile);
99
        } catch(SftpException e) {
100
	    throw new JSchException(e.toString());
101
        } finally {
102
            if (channel != null) {
103
                channel.disconnect();
104
            }
105
        }
106
        log("done\n");
107
    }
108
109
    private void getDir(ChannelSftp channel,
110
			String remoteFile,
111
			File localFile) throws IOException, SftpException {
112
	String pwd=remoteFile;
113
	if(remoteFile.lastIndexOf('/')!=-1){
114
	    if(remoteFile.length()>1)
115
  	      pwd=remoteFile.substring(0, remoteFile.lastIndexOf('/'));
116
	}
117
	channel.cd(pwd);
118
	if(!localFile.exists()){
119
	    localFile.mkdirs();
120
	}
121
        java.util.Vector files=channel.ls(remoteFile);
122
        for(int i=0; i<files.size(); i++){
123
	    ChannelSftp.LsEntry le=(ChannelSftp.LsEntry)files.elementAt(i);
124
	    String name=le.getFilename();
125
	    if(le.getAttrs().isDir()){
126
		if(name.equals(".") || name.equals("..")){
127
		    continue;
128
		}
129
		getDir(channel, 
130
		       channel.pwd()+"/"+name+"/", 
131
		       new File(localFile, le.getFilename()));
132
	    }
133
	    else{
134
		getFile(channel, le, localFile);
135
	    }
136
	}
137
	channel.cd("..");
138
    }
139
140
    private void getFile(ChannelSftp channel, 
141
			 ChannelSftp.LsEntry le,
142
			 File localFile) throws IOException, SftpException {
143
	String remoteFile=le.getFilename();
144
	if(!localFile.exists()){
145
	    String path=localFile.getAbsolutePath();
146
	    int i=0;
147
	    if((i=path.lastIndexOf(File.pathSeparator))!=-1){
148
		if(path.length()>File.pathSeparator.length())
149
		    new File(path.substring(0, i)).mkdirs();
150
	    }
151
	}
152
153
	if(localFile.isDirectory()){
154
	    localFile=new File(localFile, remoteFile);
155
	}
156
157
        long startTime = System.currentTimeMillis();
158
	long totalLength=le.getAttrs().getSize();
159
160
	ProgressMonitor monitor=null;
161
	boolean trackProgress = getVerbose() && totalLength > 102400;
162
	if(trackProgress){
163
	    monitor=getProgressMonitor();
164
	}
165
	try{
166
	    log("Receiving: " + remoteFile + " : " + le.getAttrs().getSize());
167
	    channel.get(remoteFile, localFile.getAbsolutePath(), monitor);
168
	}
169
	finally{
170
            long endTime = System.currentTimeMillis();
171
//	    if(monitor!=null && monitor.getTotalLength()<totalLength){
172
//		totalLength=monitor.getTotalLength();
173
//	    }
174
            logStats(startTime, endTime, (int)totalLength);
175
	}
176
    }
177
}
(-)ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java (+18 lines)
Lines 40-45 Link Here
40
    private List directoryList;
40
    private List directoryList;
41
41
42
    /**
42
    /**
43
     * Constructor for ScpToMessage
44
     * @param session the ssh session to use
45
     */
46
    public ScpToMessage(Session session) {
47
        super(session);
48
    }
49
50
    /**
51
     * Constructor for ScpToMessage
52
     * @param verbose if true do verbose logging
53
     * @param session the ssh session to use
54
     * @since Ant 1.6.2
55
     */
56
    public ScpToMessage(boolean verbose, Session session) {
57
	super(verbose, session);
58
    }
59
60
    /**
43
     * Constructor for a local file to remote.
61
     * Constructor for a local file to remote.
44
     * @param verbose if true do verbose logging
62
     * @param verbose if true do verbose logging
45
     * @param session the scp session to use
63
     * @param session the scp session to use
(-)ant/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessageBySftp.java (+234 lines)
Line 0 Link Here
1
/*
2
 * Copyright  2003-2005 The Apache Software Foundation
3
 *
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 *  you may not use this file except in compliance with the License.
6
 *  You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *  Unless required by applicable law or agreed to in writing, software
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *  See the License for the specific language governing permissions and
14
 *  limitations under the License.
15
 *
16
 */
17
18
package org.apache.tools.ant.taskdefs.optional.ssh;
19
20
import com.jcraft.jsch.Channel;
21
import com.jcraft.jsch.Session;
22
import com.jcraft.jsch.ChannelSftp;
23
import com.jcraft.jsch.JSchException;
24
import com.jcraft.jsch.SftpException;
25
import com.jcraft.jsch.SftpProgressMonitor;
26
import com.jcraft.jsch.SftpATTRS;
27
28
import java.io.File;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.io.FileInputStream;
32
import java.io.OutputStream;
33
import java.util.List;
34
import java.util.Iterator;
35
36
public class ScpToMessageBySftp extends ScpToMessage/*AbstractSshMessage*/ {
37
38
    private File localFile;
39
    private String remotePath;
40
    private List directoryList;
41
42
    /**
43
     * Constructor for a local file to remote.
44
     * @param verbose if true do verbose logging
45
     * @param session the scp session to use
46
     * @param aLocalFile the local file
47
     * @param aRemotePath the remote path
48
     * @since Ant 1.6.2
49
     */
50
    public ScpToMessageBySftp(boolean verbose,
51
			      Session session,
52
			      File aLocalFile,
53
			      String aRemotePath) {
54
        this(verbose, session, aRemotePath);
55
56
        this.localFile = aLocalFile;
57
    }
58
59
    /**
60
     * Constructor for a local directories to remote.
61
     * @param verbose if true do verbose logging
62
     * @param session the scp session to use
63
     * @param aDirectoryList a list of directories
64
     * @param aRemotePath the remote path
65
     * @since Ant 1.6.2
66
     */
67
    public ScpToMessageBySftp(boolean verbose,
68
			      Session session,
69
			      List aDirectoryList,
70
			      String aRemotePath) {
71
        this(verbose, session, aRemotePath);
72
73
        this.directoryList = aDirectoryList;
74
    }
75
76
    /**
77
     * Constructor for ScpToMessage.
78
     * @param verbose if true do verbose logging
79
     * @param session the scp session to use
80
     * @param aRemotePath the remote path
81
     * @since Ant 1.6.2
82
     */
83
    private ScpToMessageBySftp(boolean verbose,
84
			       Session session,
85
			       String aRemotePath) {
86
        super(verbose, session);
87
        this.remotePath = aRemotePath;
88
    }
89
90
    /**
91
     * Constructor for ScpToMessage.
92
     * @param session the scp session to use
93
     * @param aLocalFile the local file
94
     * @param aRemotePath the remote path
95
     */
96
    public ScpToMessageBySftp(Session session,
97
			      File aLocalFile,
98
			      String aRemotePath) {
99
        this(false, session, aLocalFile, aRemotePath);
100
    }
101
102
    /**
103
     * Constructor for ScpToMessage.
104
     * @param session the scp session to use
105
     * @param aDirectoryList a list of directories
106
     * @param aRemotePath the remote path
107
     */
108
    public ScpToMessageBySftp(Session session,
109
			      List aDirectoryList,
110
			      String aRemotePath) {
111
        this(false, session, aDirectoryList, aRemotePath);
112
    }
113
114
    /**
115
     * Carry out the transfer.
116
     * @throws IOException on i/o errors
117
     * @throws JSchException on errors detected by scp
118
     */
119
    public void execute() throws IOException, JSchException {
120
        if (directoryList != null) {
121
            doMultipleTransfer();
122
        }
123
        if (localFile != null) {
124
            doSingleTransfer();
125
        }
126
        log("done.\n");
127
    }
128
129
    private void doSingleTransfer() throws IOException, JSchException {
130
        ChannelSftp channel = openSftpChannel();
131
        try {
132
            channel.connect();
133
	    try{
134
		sendFileToRemote(channel, localFile, remotePath);
135
	    }
136
	    catch(SftpException e){
137
		throw new JSchException(e.toString());
138
	    }
139
        } finally {
140
            if (channel != null) {
141
                channel.disconnect();
142
            }
143
        }
144
    }
145
146
    private void doMultipleTransfer() throws IOException, JSchException {
147
        ChannelSftp channel = openSftpChannel();
148
        try {
149
            channel.connect();
150
151
	    try{
152
		channel.cd(remotePath);
153
		for (Iterator i = directoryList.iterator(); i.hasNext();) {
154
		    Directory current = (Directory) i.next();
155
		    sendDirectory(channel, current);
156
		}
157
	    }
158
	    catch(SftpException e){
159
		throw new JSchException(e.toString());
160
	    }
161
        } finally {
162
            if (channel != null) {
163
                channel.disconnect();
164
            }
165
        }
166
    }
167
168
    private void sendDirectory(ChannelSftp channel,
169
			       Directory current) throws IOException, SftpException {
170
        for (Iterator fileIt = current.filesIterator(); fileIt.hasNext();) {
171
            sendFileToRemote(channel, (File) fileIt.next(), null);
172
        }
173
        for (Iterator dirIt = current.directoryIterator(); dirIt.hasNext();) {
174
            Directory dir = (Directory) dirIt.next();
175
            sendDirectoryToRemote(channel, dir);
176
        }
177
    }
178
179
    private void sendDirectoryToRemote(ChannelSftp channel,
180
				       Directory directory) throws IOException, SftpException {
181
	String dir=directory.getDirectory().getName();
182
	try{
183
	    channel.stat(dir);
184
	}
185
	catch(SftpException e){ 
186
	    // dir does not exist.
187
	    if(e.id==ChannelSftp.SSH_FX_NO_SUCH_FILE)
188
		channel.mkdir(dir);
189
	}
190
	channel.cd(dir);
191
	sendDirectory(channel, directory);
192
	channel.cd("..");
193
    }
194
195
    private void sendFileToRemote(ChannelSftp channel,
196
				  File localFile,
197
                                  String remotePath) throws IOException, SftpException {
198
        long filesize = localFile.length();
199
200
	if(remotePath==null){
201
	    remotePath=localFile.getName();
202
	}
203
204
        long startTime = System.currentTimeMillis();
205
	long totalLength=filesize;
206
207
        // only track progress for files larger than 100kb in verbose mode
208
        boolean trackProgress = getVerbose() && filesize > 102400;
209
210
	ProgressMonitor monitor=null;
211
	if(trackProgress){
212
	    monitor=getProgressMonitor();
213
	}
214
215
	try{
216
	    log("Sending: " + localFile.getName() + " : " + filesize);
217
	    channel.put(localFile.getAbsolutePath(), remotePath, monitor);
218
	}
219
	finally {
220
            long endTime = System.currentTimeMillis();
221
//	    if(monitor!=null && monitor.getTotalLength()<totalLength){
222
//		totalLength=monitor.getTotalLength();
223
//	    }
224
            logStats(startTime, endTime, (int)totalLength);
225
	}
226
    }
227
    public File getLocalFile() {
228
        return localFile;
229
    }
230
231
    public String getRemotePath() {
232
        return remotePath;
233
    }
234
}

Return to bug 39373