File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\ssh\SFTPClient.java.html

1 package hudson.util.ssh;
2
3 import com.trilead.ssh2.Connection;
4 import com.trilead.ssh2.SFTPException;
5 import com.trilead.ssh2.SFTPv3Client;
6 import com.trilead.ssh2.SFTPv3FileAttributes;
7 import com.trilead.ssh2.SFTPv3FileHandle;
8 import com.trilead.ssh2.sftp.ErrorCodes;
9 import hudson.util.IOException2;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14
15 /**
16 * Improved {@link SFTPv3Client}.
17 *
18 * @author Kohsuke Kawaguchi
19 * @since 1.339
20 */
21 public class SFTPClient extends SFTPv3Client {
22 public SFTPClient(Connection conn) throws IOException {
23 super(conn);
24 }
25
26 /**
27 * Checks if the given path exists.
28 */
29 public boolean exists(String path) throws IOException {
30 return _stat(path)!=null;
31 }
32
33 /**
34 * Graceful {@link #stat(String)} that returns null if the path doesn't exist.
35 */
36 public SFTPv3FileAttributes _stat(String path) throws IOException {
37 try {
38 return stat(path);
39 } catch (SFTPException e) {
40 int c = e.getServerErrorCode();
41 if (c== ErrorCodes.SSH_FX_NO_SUCH_FILE || c==ErrorCodes.SSH_FX_NO_SUCH_PATH)
42 return null;
43 else
44 throw e;
45 }
46 }
47
48 /**
49 * Makes sure that the directory exists, by creating it if necessary.
50 */
51 public void mkdirs(String path, int posixPermission) throws IOException {
52 SFTPv3FileAttributes atts = _stat(path);
53 if (atts!=null && atts.isDirectory())
54 return;
55
56 int idx = path.lastIndexOf("/");
57 if (idx>0)
58 mkdirs(path.substring(0,idx), posixPermission);
59
60 try {
61 mkdir(path, posixPermission);
62 } catch (IOException e) {
63 throw new IOException2("Failed to mkdir "+path,e);
64 }
65 }
66
67 /**
68 * Creates a new file and writes to it.
69 */
70 public OutputStream writeToFile(String path) throws IOException {
71 final SFTPv3FileHandle h = createFile(path);
72 return new OutputStream() {
73 private long offset = 0;
74 public void write(int b) throws IOException {
75 write(new byte[]{(byte)b});
76 }
77
78 @Override
79 public void write(byte[] b, int off, int len) throws IOException {
80 SFTPClient.this.write(h,offset,b,off,len);
81 offset += len;
82 }
83
84 @Override
85 public void close() throws IOException {
86 closeFile(h);
87 }
88 };
89 }
90
91 public InputStream read(String file) throws IOException {
92 final SFTPv3FileHandle h = openFileRO(file);
93 return new InputStream() {
94 private long offset = 0;
95
96 public int read() throws IOException {
97 byte[] b = new byte[1];
98 if(read(b)<0)
99 return -1;
100 return b[0];
101 }
102
103 @Override
104 public int read(byte[] b, int off, int len) throws IOException {
105 int r = SFTPClient.this.read(h,offset,b,off,len);
106 if (r<0) return -1;
107 offset += r;
108 return r;
109 }
110
111 @Override
112 public long skip(long n) throws IOException {
113 offset += n;
114 return n;
115 }
116
117 @Override
118 public void close() throws IOException {
119 closeFile(h);
120 }
121 };
122 }
123
124 public void chmod(String path, int permissions) throws IOException {
125 SFTPv3FileAttributes atts = new SFTPv3FileAttributes();
126 atts.permissions = permissions;
127 setstat(path, atts);
128 }
129
130 }
131