1 |
|
package org.apache.maven.wagon.providers.ssh; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import com.jcraft.jsch.ChannelExec; |
20 |
|
import com.jcraft.jsch.JSchException; |
21 |
|
import org.apache.maven.wagon.CommandExecutionException; |
22 |
|
import org.apache.maven.wagon.PathUtils; |
23 |
|
import org.apache.maven.wagon.PermissionModeUtils; |
24 |
|
import org.apache.maven.wagon.ResourceDoesNotExistException; |
25 |
|
import org.apache.maven.wagon.TransferFailedException; |
26 |
|
import org.apache.maven.wagon.authorization.AuthorizationException; |
27 |
|
import org.apache.maven.wagon.repository.RepositoryPermissions; |
28 |
|
import org.apache.maven.wagon.resource.Resource; |
29 |
|
import org.codehaus.plexus.util.IOUtil; |
30 |
|
import org.codehaus.plexus.util.StringUtils; |
31 |
|
|
32 |
|
import java.io.EOFException; |
33 |
|
import java.io.File; |
34 |
|
import java.io.IOException; |
35 |
|
import java.io.InputStream; |
36 |
|
import java.io.OutputStream; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
0 |
public class ScpWagon |
51 |
|
extends AbstractSshWagon |
52 |
|
{ |
53 |
|
private static final char PATH_SEPARATOR = '/'; |
54 |
|
|
55 |
|
private static final char COPY_START_CHAR = 'C'; |
56 |
|
|
57 |
|
private static final char ACK_SEPARATOR = ' '; |
58 |
|
|
59 |
|
private static final String END_OF_FILES_MSG = "E\n"; |
60 |
|
|
61 |
|
public void put( File source, String resourceName ) |
62 |
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
63 |
|
{ |
64 |
0 |
String basedir = getRepository().getBasedir(); |
65 |
|
|
66 |
0 |
resourceName = StringUtils.replace( resourceName, "\\", "/" ); |
67 |
0 |
String dir = PathUtils.dirname( resourceName ); |
68 |
0 |
dir = StringUtils.replace( dir, "\\", "/" ); |
69 |
|
|
70 |
0 |
Resource resource = new Resource( resourceName ); |
71 |
|
|
72 |
0 |
firePutInitiated( resource, source ); |
73 |
|
|
74 |
|
try |
75 |
|
{ |
76 |
0 |
String umaskCmd = null; |
77 |
0 |
if ( getRepository().getPermissions() != null ) |
78 |
|
{ |
79 |
0 |
String dirPerms = getRepository().getPermissions().getDirectoryMode(); |
80 |
|
|
81 |
0 |
if ( dirPerms != null ) |
82 |
|
{ |
83 |
0 |
umaskCmd = "umask " + PermissionModeUtils.getUserMaskFor( dirPerms ); |
84 |
|
} |
85 |
|
} |
86 |
|
|
87 |
0 |
String mkdirCmd = "mkdir -p " + getPath( basedir, dir ); |
88 |
|
|
89 |
0 |
if ( umaskCmd != null ) |
90 |
|
{ |
91 |
0 |
mkdirCmd = umaskCmd + "; " + mkdirCmd; |
92 |
|
} |
93 |
|
|
94 |
0 |
executeCommand( mkdirCmd ); |
95 |
|
} |
96 |
0 |
catch ( CommandExecutionException e ) |
97 |
|
{ |
98 |
0 |
throw new TransferFailedException( "Error performing commands for file transfer", e ); |
99 |
0 |
} |
100 |
|
|
101 |
0 |
ChannelExec channel = null; |
102 |
|
|
103 |
0 |
OutputStream out = null; |
104 |
|
|
105 |
0 |
String path = getPath( basedir, resourceName ); |
106 |
|
|
107 |
0 |
RepositoryPermissions permissions = getRepository().getPermissions(); |
108 |
|
|
109 |
|
try |
110 |
|
{ |
111 |
|
|
112 |
0 |
String command = "scp -t " + path; |
113 |
|
|
114 |
0 |
fireTransferDebug( "Executing command: " + command ); |
115 |
|
|
116 |
0 |
channel = (ChannelExec) session.openChannel( EXEC_CHANNEL ); |
117 |
|
|
118 |
0 |
channel.setCommand( command ); |
119 |
|
|
120 |
|
|
121 |
0 |
out = channel.getOutputStream(); |
122 |
|
|
123 |
0 |
InputStream in = channel.getInputStream(); |
124 |
|
|
125 |
0 |
channel.connect(); |
126 |
|
|
127 |
0 |
checkAck( in ); |
128 |
|
|
129 |
|
|
130 |
0 |
long filesize = source.length(); |
131 |
|
|
132 |
0 |
String mode = "644"; |
133 |
0 |
if ( permissions != null && permissions.getFileMode() != null ) |
134 |
|
{ |
135 |
0 |
if ( permissions.getFileMode().matches( "[0-9]{3}" ) ) |
136 |
|
{ |
137 |
0 |
mode = permissions.getFileMode(); |
138 |
0 |
} |
139 |
|
else |
140 |
|
{ |
141 |
|
|
142 |
0 |
fireSessionDebug( "Not using non-octal permissions: " + mode ); |
143 |
|
} |
144 |
|
} |
145 |
|
|
146 |
0 |
command = "C0" + mode + " " + filesize + " "; |
147 |
|
|
148 |
0 |
if ( resourceName.lastIndexOf( PATH_SEPARATOR ) > 0 ) |
149 |
|
{ |
150 |
0 |
command += resourceName.substring( resourceName.lastIndexOf( PATH_SEPARATOR ) + 1 ); |
151 |
0 |
} |
152 |
|
else |
153 |
|
{ |
154 |
0 |
command += resourceName; |
155 |
|
} |
156 |
|
|
157 |
0 |
command += "\n"; |
158 |
|
|
159 |
0 |
out.write( command.getBytes() ); |
160 |
|
|
161 |
0 |
out.flush(); |
162 |
|
|
163 |
0 |
checkAck( in ); |
164 |
|
|
165 |
0 |
putTransfer( resource, source, out, false ); |
166 |
|
|
167 |
0 |
sendEom( out ); |
168 |
|
|
169 |
0 |
checkAck( in ); |
170 |
|
|
171 |
|
|
172 |
0 |
out.write( END_OF_FILES_MSG.getBytes() ); |
173 |
0 |
out.flush(); |
174 |
|
} |
175 |
0 |
catch ( IOException e ) |
176 |
|
{ |
177 |
0 |
String msg = "Error occured while deploying '" + resourceName + "' to remote repository: " + |
178 |
|
getRepository().getUrl(); |
179 |
|
|
180 |
0 |
throw new TransferFailedException( msg, e ); |
181 |
|
} |
182 |
0 |
catch ( JSchException e ) |
183 |
|
{ |
184 |
0 |
String msg = "Error occured while deploying '" + resourceName + "' to remote repository: " + |
185 |
|
getRepository().getUrl(); |
186 |
|
|
187 |
0 |
throw new TransferFailedException( msg, e ); |
188 |
|
} |
189 |
|
finally |
190 |
|
{ |
191 |
0 |
if ( channel != null ) |
192 |
|
{ |
193 |
0 |
IOUtil.close( out ); |
194 |
|
|
195 |
0 |
channel.disconnect(); |
196 |
0 |
} |
197 |
0 |
} |
198 |
|
|
199 |
|
try |
200 |
|
{ |
201 |
0 |
if ( permissions != null && permissions.getGroup() != null ) |
202 |
|
{ |
203 |
0 |
executeCommand( "chgrp -f " + permissions.getGroup() + " " + path ); |
204 |
|
} |
205 |
|
} |
206 |
0 |
catch ( CommandExecutionException e ) |
207 |
|
{ |
208 |
0 |
throw new TransferFailedException( "Error performing commands for file transfer", e ); |
209 |
0 |
} |
210 |
0 |
} |
211 |
|
|
212 |
|
private void checkAck( InputStream in ) |
213 |
|
throws IOException, TransferFailedException |
214 |
|
{ |
215 |
0 |
int code = in.read(); |
216 |
0 |
if ( code == -1 ) |
217 |
|
{ |
218 |
0 |
throw new TransferFailedException( "Unexpected end of data" ); |
219 |
|
} |
220 |
0 |
else if ( code == 1 ) |
221 |
|
{ |
222 |
0 |
String line = readLine( in ); |
223 |
|
|
224 |
0 |
throw new TransferFailedException( "SCP terminated with error: '" + line + "'" ); |
225 |
|
} |
226 |
0 |
else if ( code == 2 ) |
227 |
|
{ |
228 |
0 |
throw new TransferFailedException( "SCP terminated with error (code: " + code + ")" ); |
229 |
|
} |
230 |
0 |
else if ( code != 0 ) |
231 |
|
{ |
232 |
0 |
throw new TransferFailedException( "SCP terminated with unknown error code" ); |
233 |
|
} |
234 |
0 |
} |
235 |
|
|
236 |
|
public void get( String resourceName, File destination ) |
237 |
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
238 |
|
{ |
239 |
0 |
Resource resource = new Resource( resourceName ); |
240 |
|
|
241 |
0 |
fireGetInitiated( resource, destination ); |
242 |
|
|
243 |
0 |
ChannelExec channel = null; |
244 |
|
|
245 |
|
|
246 |
0 |
OutputStream out = null; |
247 |
|
|
248 |
|
InputStream in; |
249 |
|
|
250 |
0 |
String basedir = getRepository().getBasedir(); |
251 |
|
|
252 |
|
try |
253 |
|
{ |
254 |
0 |
String path = getPath( basedir, resourceName ); |
255 |
0 |
String cmd = "scp -f " + path; |
256 |
|
|
257 |
0 |
fireTransferDebug( "Executing command: " + cmd ); |
258 |
|
|
259 |
0 |
channel = (ChannelExec) session.openChannel( EXEC_CHANNEL ); |
260 |
|
|
261 |
0 |
channel.setCommand( cmd ); |
262 |
|
|
263 |
|
|
264 |
0 |
out = channel.getOutputStream(); |
265 |
|
|
266 |
0 |
in = channel.getInputStream(); |
267 |
|
|
268 |
0 |
channel.connect(); |
269 |
|
|
270 |
0 |
sendEom( out ); |
271 |
|
|
272 |
0 |
int exitCode = in.read(); |
273 |
|
|
274 |
0 |
if ( exitCode == 'P' ) |
275 |
|
{ |
276 |
|
|
277 |
|
|
278 |
0 |
exitCode = in.read(); |
279 |
|
} |
280 |
|
|
281 |
0 |
String line = readLine( in ); |
282 |
|
|
283 |
0 |
if ( exitCode != COPY_START_CHAR ) |
284 |
|
{ |
285 |
0 |
if ( exitCode == 1 && line.endsWith( "No such file or directory" ) ) |
286 |
|
{ |
287 |
0 |
throw new ResourceDoesNotExistException( line ); |
288 |
|
} |
289 |
|
else |
290 |
|
{ |
291 |
0 |
throw new TransferFailedException( "Exit code: " + exitCode + " - " + line ); |
292 |
|
} |
293 |
|
} |
294 |
|
|
295 |
0 |
if ( line == null ) |
296 |
|
{ |
297 |
0 |
throw new EOFException( "Unexpected end of data" ); |
298 |
|
} |
299 |
|
|
300 |
0 |
String perms = line.substring( 0, 4 ); |
301 |
0 |
fireTransferDebug( "Remote file permissions: " + perms ); |
302 |
|
|
303 |
0 |
if ( line.charAt( 4 ) != ACK_SEPARATOR && line.charAt( 5 ) != ACK_SEPARATOR ) |
304 |
|
{ |
305 |
0 |
throw new TransferFailedException( "Invalid transfer header: " + line ); |
306 |
|
} |
307 |
|
|
308 |
0 |
int index = line.indexOf( ACK_SEPARATOR, 5 ); |
309 |
0 |
if ( index < 0 ) |
310 |
|
{ |
311 |
0 |
throw new TransferFailedException( "Invalid transfer header: " + line ); |
312 |
|
} |
313 |
|
|
314 |
0 |
int filesize = Integer.valueOf( line.substring( 5, index ) ).intValue(); |
315 |
0 |
fireTransferDebug( "Remote file size: " + filesize ); |
316 |
|
|
317 |
0 |
resource.setContentLength( filesize ); |
318 |
|
|
319 |
0 |
String filename = line.substring( index + 1 ); |
320 |
0 |
fireTransferDebug( "Remote filename: " + filename ); |
321 |
|
|
322 |
0 |
sendEom( out ); |
323 |
|
|
324 |
0 |
getTransfer( resource, destination, in, false, filesize ); |
325 |
|
|
326 |
0 |
if ( destination.length() != filesize ) |
327 |
|
{ |
328 |
0 |
throw new TransferFailedException( |
329 |
|
"Expected file length: " + filesize + "; received = " + destination.length() ); |
330 |
|
} |
331 |
|
|
332 |
|
|
333 |
|
|
334 |
0 |
checkAck( in ); |
335 |
|
|
336 |
0 |
sendEom( out ); |
337 |
|
} |
338 |
0 |
catch ( JSchException e ) |
339 |
|
{ |
340 |
0 |
handleGetException( resource, e, destination ); |
341 |
|
} |
342 |
0 |
catch ( IOException e ) |
343 |
|
{ |
344 |
0 |
handleGetException( resource, e, destination ); |
345 |
|
} |
346 |
|
finally |
347 |
|
{ |
348 |
0 |
IOUtil.close( out ); |
349 |
|
|
350 |
0 |
if ( channel != null ) |
351 |
|
{ |
352 |
0 |
channel.disconnect(); |
353 |
0 |
} |
354 |
0 |
} |
355 |
0 |
} |
356 |
|
|
357 |
|
public boolean getIfNewer( String resourceName, File destination, long timestamp ) |
358 |
|
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
359 |
|
{ |
360 |
0 |
throw new UnsupportedOperationException( "getIfNewer is scp wagon must be still implemented" ); |
361 |
|
} |
362 |
|
|
363 |
|
} |