comparison src/ch/ethz/ssh2/SFTPv6Client.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents d2b303406d63
children
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
23 public SFTPv6Client(Connection conn) throws IOException { 23 public SFTPv6Client(Connection conn) throws IOException {
24 this(conn, new PacketListener() { 24 this(conn, new PacketListener() {
25 public void read(String packet) { 25 public void read(String packet) {
26 log.debug("Read packet " + packet); 26 log.debug("Read packet " + packet);
27 } 27 }
28
29 public void write(String packet) { 28 public void write(String packet) {
30 log.debug("Write packet " + packet); 29 log.debug("Write packet " + packet);
31 } 30 }
32 }); 31 });
33 } 32 }
37 this.listener = listener; 36 this.listener = listener;
38 } 37 }
39 38
40 public SFTPv6FileAttributes fstat(SFTPFileHandle handle) throws IOException { 39 public SFTPv6FileAttributes fstat(SFTPFileHandle handle) throws IOException {
41 int req_id = generateNextRequestID(); 40 int req_id = generateNextRequestID();
42
43 TypesWriter tw = new TypesWriter(); 41 TypesWriter tw = new TypesWriter();
44 tw.writeString(handle.getHandle(), 0, handle.getHandle().length); 42 tw.writeString(handle.getHandle(), 0, handle.getHandle().length);
45
46 sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes()); 43 sendMessage(Packet.SSH_FXP_FSTAT, req_id, tw.getBytes());
47
48 byte[] resp = receiveMessage(34000); 44 byte[] resp = receiveMessage(34000);
49
50 TypesReader tr = new TypesReader(resp); 45 TypesReader tr = new TypesReader(resp);
51
52 int t = tr.readByte(); 46 int t = tr.readByte();
53 listener.read(Packet.forName(t)); 47 listener.read(Packet.forName(t));
54
55 int rep_id = tr.readUINT32(); 48 int rep_id = tr.readUINT32();
56 if(rep_id != req_id) { 49
50 if (rep_id != req_id) {
57 throw new RequestMismatchException(); 51 throw new RequestMismatchException();
58 } 52 }
59 53
60 if(t == Packet.SSH_FXP_ATTRS) { 54 if (t == Packet.SSH_FXP_ATTRS) {
61 return new SFTPv6FileAttributes(tr); 55 return new SFTPv6FileAttributes(tr);
62 } 56 }
63 57
64 if(t != Packet.SSH_FXP_STATUS) { 58 if (t != Packet.SSH_FXP_STATUS) {
65 throw new PacketTypeException(t); 59 throw new PacketTypeException(t);
66 } 60 }
67 61
68 int errorCode = tr.readUINT32(); 62 int errorCode = tr.readUINT32();
69 String errorMessage = tr.readString(); 63 String errorMessage = tr.readString();
71 throw new SFTPException(errorMessage, errorCode); 65 throw new SFTPException(errorMessage, errorCode);
72 } 66 }
73 67
74 private SFTPv6FileAttributes statBoth(String path, int statMethod) throws IOException { 68 private SFTPv6FileAttributes statBoth(String path, int statMethod) throws IOException {
75 int req_id = generateNextRequestID(); 69 int req_id = generateNextRequestID();
76
77 TypesWriter tw = new TypesWriter(); 70 TypesWriter tw = new TypesWriter();
78 tw.writeString(path, this.getCharset()); 71 tw.writeString(path, this.getCharset());
79
80 sendMessage(statMethod, req_id, tw.getBytes()); 72 sendMessage(statMethod, req_id, tw.getBytes());
81
82 byte[] resp = receiveMessage(34000); 73 byte[] resp = receiveMessage(34000);
83
84 TypesReader tr = new TypesReader(resp); 74 TypesReader tr = new TypesReader(resp);
85
86 int t = tr.readByte(); 75 int t = tr.readByte();
87 listener.read(Packet.forName(t)); 76 listener.read(Packet.forName(t));
88
89 int rep_id = tr.readUINT32(); 77 int rep_id = tr.readUINT32();
90 if(rep_id != req_id) { 78
79 if (rep_id != req_id) {
91 throw new RequestMismatchException(); 80 throw new RequestMismatchException();
92 } 81 }
93 82
94 if(t == Packet.SSH_FXP_ATTRS) { 83 if (t == Packet.SSH_FXP_ATTRS) {
95 return new SFTPv6FileAttributes(tr); 84 return new SFTPv6FileAttributes(tr);
96 } 85 }
97 86
98 if(t != Packet.SSH_FXP_STATUS) { 87 if (t != Packet.SSH_FXP_STATUS) {
99 throw new PacketTypeException(t); 88 throw new PacketTypeException(t);
100 } 89 }
101 90
102 int errorCode = tr.readUINT32(); 91 int errorCode = tr.readUINT32();
103 String errorMessage = tr.readString(); 92 String errorMessage = tr.readString();
115 104
116 105
117 private List<SFTPv6DirectoryEntry> scanDirectory(byte[] handle) throws IOException { 106 private List<SFTPv6DirectoryEntry> scanDirectory(byte[] handle) throws IOException {
118 List<SFTPv6DirectoryEntry> files = new ArrayList<SFTPv6DirectoryEntry>(); 107 List<SFTPv6DirectoryEntry> files = new ArrayList<SFTPv6DirectoryEntry>();
119 108
120 while(true) { 109 while (true) {
121 int req_id = generateNextRequestID(); 110 int req_id = generateNextRequestID();
122
123 TypesWriter tw = new TypesWriter(); 111 TypesWriter tw = new TypesWriter();
124 tw.writeString(handle, 0, handle.length); 112 tw.writeString(handle, 0, handle.length);
125
126 sendMessage(Packet.SSH_FXP_READDIR, req_id, tw.getBytes()); 113 sendMessage(Packet.SSH_FXP_READDIR, req_id, tw.getBytes());
127
128 byte[] resp = receiveMessage(34000); 114 byte[] resp = receiveMessage(34000);
129
130 TypesReader tr = new TypesReader(resp); 115 TypesReader tr = new TypesReader(resp);
131
132 int t = tr.readByte(); 116 int t = tr.readByte();
133 listener.read(Packet.forName(t)); 117 listener.read(Packet.forName(t));
134 int rep_id = tr.readUINT32(); 118 int rep_id = tr.readUINT32();
135 if(rep_id != req_id) { 119
120 if (rep_id != req_id) {
136 throw new RequestMismatchException(); 121 throw new RequestMismatchException();
137 } 122 }
138 if(t == Packet.SSH_FXP_NAME) { 123
124 if (t == Packet.SSH_FXP_NAME) {
139 int count = tr.readUINT32(); 125 int count = tr.readUINT32();
140 if(log.isDebugEnabled()) { 126
127 if (log.isDebugEnabled()) {
141 log.debug(String.format("Parsing %d name entries", count)); 128 log.debug(String.format("Parsing %d name entries", count));
142 } 129 }
143 while(count > 0) { 130
131 while (count > 0) {
144 SFTPv6DirectoryEntry file = new SFTPv6DirectoryEntry(); 132 SFTPv6DirectoryEntry file = new SFTPv6DirectoryEntry();
145 file.filename = tr.readString(this.getCharset()); 133 file.filename = tr.readString(this.getCharset());
146 listener.read(file.filename); 134 listener.read(file.filename);
147 file.attributes = new SFTPv6FileAttributes(tr); 135 file.attributes = new SFTPv6FileAttributes(tr);
148 if(log.isDebugEnabled()) { 136
137 if (log.isDebugEnabled()) {
149 log.debug(String.format("Adding file %s", file)); 138 log.debug(String.format("Adding file %s", file));
150 } 139 }
140
151 files.add(file); 141 files.add(file);
152 count--; 142 count--;
153 } 143 }
144
154 continue; 145 continue;
155 } 146 }
156 if(t != Packet.SSH_FXP_STATUS) { 147
148 if (t != Packet.SSH_FXP_STATUS) {
157 throw new PacketTypeException(t); 149 throw new PacketTypeException(t);
158 } 150 }
151
159 int errorCode = tr.readUINT32(); 152 int errorCode = tr.readUINT32();
160 if(errorCode == ErrorCodes.SSH_FX_EOF) { 153
154 if (errorCode == ErrorCodes.SSH_FX_EOF) {
161 return files; 155 return files;
162 } 156 }
157
163 String errorMessage = tr.readString(); 158 String errorMessage = tr.readString();
164 listener.read(errorMessage); 159 listener.read(errorMessage);
165 throw new SFTPException(errorMessage, errorCode); 160 throw new SFTPException(errorMessage, errorCode);
166 } 161 }
167 } 162 }
168 163
169 public final SFTPFileHandle openDirectory(String path) throws IOException { 164 public final SFTPFileHandle openDirectory(String path) throws IOException {
170 int req_id = generateNextRequestID(); 165 int req_id = generateNextRequestID();
171
172 TypesWriter tw = new TypesWriter(); 166 TypesWriter tw = new TypesWriter();
173 tw.writeString(path, this.getCharset()); 167 tw.writeString(path, this.getCharset());
174
175 sendMessage(Packet.SSH_FXP_OPENDIR, req_id, tw.getBytes()); 168 sendMessage(Packet.SSH_FXP_OPENDIR, req_id, tw.getBytes());
176
177 byte[] resp = receiveMessage(34000); 169 byte[] resp = receiveMessage(34000);
178
179 TypesReader tr = new TypesReader(resp); 170 TypesReader tr = new TypesReader(resp);
180
181 int t = tr.readByte(); 171 int t = tr.readByte();
182 listener.read(Packet.forName(t)); 172 listener.read(Packet.forName(t));
183
184 int rep_id = tr.readUINT32(); 173 int rep_id = tr.readUINT32();
185 if(rep_id != req_id) { 174
175 if (rep_id != req_id) {
186 throw new RequestMismatchException(); 176 throw new RequestMismatchException();
187 } 177 }
188 178
189 if(t == Packet.SSH_FXP_HANDLE) { 179 if (t == Packet.SSH_FXP_HANDLE) {
190 return new SFTPFileHandle(this, tr.readByteString()); 180 return new SFTPFileHandle(this, tr.readByteString());
191 } 181 }
192 182
193 if(t != Packet.SSH_FXP_STATUS) { 183 if (t != Packet.SSH_FXP_STATUS) {
194 throw new PacketTypeException(t); 184 throw new PacketTypeException(t);
195 } 185 }
196 186
197 int errorCode = tr.readUINT32(); 187 int errorCode = tr.readUINT32();
198 String errorMessage = tr.readString(); 188 String errorMessage = tr.readString();
249 } 239 }
250 240
251 @Override 241 @Override
252 public SFTPFileHandle openFile(String filename, int flags, SFTPFileAttributes attr) throws IOException { 242 public SFTPFileHandle openFile(String filename, int flags, SFTPFileAttributes attr) throws IOException {
253 int req_id = generateNextRequestID(); 243 int req_id = generateNextRequestID();
254
255 TypesWriter tw = new TypesWriter(); 244 TypesWriter tw = new TypesWriter();
256 tw.writeString(filename, this.getCharset()); 245 tw.writeString(filename, this.getCharset());
257 tw.writeUINT32(AceMask.ACE4_READ_DATA | AceMask.ACE4_READ_ATTRIBUTES | AceMask.ACE4_READ_ACL | AceMask.ACE4_READ_NAMED_ATTRS 246 tw.writeUINT32(AceMask.ACE4_READ_DATA | AceMask.ACE4_READ_ATTRIBUTES | AceMask.ACE4_READ_ACL | AceMask.ACE4_READ_NAMED_ATTRS
258 | AceMask.ACE4_WRITE_DATA | AceMask.ACE4_APPEND_DATA | AceMask.ACE4_WRITE_ATTRIBUTES | AceMask.ACE4_WRITE_ACL | AceMask.ACE4_WRITE_NAMED_ATTRS); 247 | AceMask.ACE4_WRITE_DATA | AceMask.ACE4_APPEND_DATA | AceMask.ACE4_WRITE_ATTRIBUTES | AceMask.ACE4_WRITE_ACL | AceMask.ACE4_WRITE_NAMED_ATTRS);
259 tw.writeUINT32(flags); 248 tw.writeUINT32(flags);
260
261 tw.writeBytes(attr.toBytes()); 249 tw.writeBytes(attr.toBytes());
262
263 sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes()); 250 sendMessage(Packet.SSH_FXP_OPEN, req_id, tw.getBytes());
264
265 byte[] resp = receiveMessage(34000); 251 byte[] resp = receiveMessage(34000);
266
267 TypesReader tr = new TypesReader(resp); 252 TypesReader tr = new TypesReader(resp);
268
269 int t = tr.readByte(); 253 int t = tr.readByte();
270 listener.read(Packet.forName(t)); 254 listener.read(Packet.forName(t));
271
272 int rep_id = tr.readUINT32(); 255 int rep_id = tr.readUINT32();
273 if(rep_id != req_id) { 256
257 if (rep_id != req_id) {
274 throw new RequestMismatchException(); 258 throw new RequestMismatchException();
275 } 259 }
276 260
277 if(t == Packet.SSH_FXP_HANDLE) { 261 if (t == Packet.SSH_FXP_HANDLE) {
278 return new SFTPFileHandle(this, tr.readByteString()); 262 return new SFTPFileHandle(this, tr.readByteString());
279 } 263 }
280 264
281 if(t != Packet.SSH_FXP_STATUS) { 265 if (t != Packet.SSH_FXP_STATUS) {
282 throw new PacketTypeException(t); 266 throw new PacketTypeException(t);
283 } 267 }
284 268
285 int errorCode = tr.readUINT32(); 269 int errorCode = tr.readUINT32();
286 String errorMessage = tr.readString(); 270 String errorMessage = tr.readString();
289 } 273 }
290 274
291 @Override 275 @Override
292 public void createSymlink(String src, String target) throws IOException { 276 public void createSymlink(String src, String target) throws IOException {
293 int req_id = generateNextRequestID(); 277 int req_id = generateNextRequestID();
294
295 TypesWriter tw = new TypesWriter(); 278 TypesWriter tw = new TypesWriter();
296 // new-link-path 279 // new-link-path
297 tw.writeString(src, this.getCharset()); 280 tw.writeString(src, this.getCharset());
298 // existing-path 281 // existing-path
299 tw.writeString(target, this.getCharset()); 282 tw.writeString(target, this.getCharset());
300 tw.writeBoolean(true); 283 tw.writeBoolean(true);
301
302 sendMessage(Packet.SSH_FXP_LINK, req_id, tw.getBytes()); 284 sendMessage(Packet.SSH_FXP_LINK, req_id, tw.getBytes());
303
304 expectStatusOKMessage(req_id); 285 expectStatusOKMessage(req_id);
305 } 286 }
306 287
307 @Override 288 @Override
308 public void createHardlink(String src, String target) throws IOException { 289 public void createHardlink(String src, String target) throws IOException {
309 int req_id = generateNextRequestID(); 290 int req_id = generateNextRequestID();
310
311 TypesWriter tw = new TypesWriter(); 291 TypesWriter tw = new TypesWriter();
312 // new-link-path 292 // new-link-path
313 tw.writeString(src, this.getCharset()); 293 tw.writeString(src, this.getCharset());
314 // existing-path 294 // existing-path
315 tw.writeString(target, this.getCharset()); 295 tw.writeString(target, this.getCharset());
316 tw.writeBoolean(false); 296 tw.writeBoolean(false);
317
318 sendMessage(Packet.SSH_FXP_LINK, req_id, tw.getBytes()); 297 sendMessage(Packet.SSH_FXP_LINK, req_id, tw.getBytes());
319
320 expectStatusOKMessage(req_id); 298 expectStatusOKMessage(req_id);
321 } 299 }
322 } 300 }