001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.daemon.sftp;
027:
028: import com.sshtools.daemon.platform.*;
029: import com.sshtools.daemon.session.*;
030: import com.sshtools.daemon.subsystem.*;
031:
032: import com.sshtools.j2ssh.*;
033: import com.sshtools.j2ssh.connection.*;
034: import com.sshtools.j2ssh.io.*;
035: import com.sshtools.j2ssh.sftp.*;
036: import com.sshtools.j2ssh.subsystem.*;
037:
038: import org.apache.commons.logging.*;
039:
040: import java.io.*;
041:
042: /**
043: *
044: *
045: * @author $author$
046: * @version $Revision: 1.15 $
047: */
048: public class SftpSubsystemServer extends SubsystemServer {
049: /** */
050: public static final int VERSION_1 = 1;
051:
052: /** */
053: public static final int VERSION_2 = 2;
054:
055: /** */
056: public static final int VERSION_3 = 3;
057:
058: /** */
059: public static final int VERSION_4 = 4;
060: private static Log log = LogFactory
061: .getLog(SftpSubsystemServer.class);
062: private NativeFileSystemProvider nfs;
063:
064: /**
065: * Creates a new SftpSubsystemServer object.
066: */
067: public SftpSubsystemServer() {
068: registerMessage(SshFxpInit.SSH_FXP_INIT, SshFxpInit.class);
069: registerMessage(SshFxpMkdir.SSH_FXP_MKDIR, SshFxpMkdir.class);
070: registerMessage(SshFxpRealPath.SSH_FXP_REALPATH,
071: SshFxpRealPath.class);
072: registerMessage(SshFxpOpenDir.SSH_FXP_OPENDIR,
073: SshFxpOpenDir.class);
074: registerMessage(SshFxpOpen.SSH_FXP_OPEN, SshFxpOpen.class);
075: registerMessage(SshFxpRead.SSH_FXP_READ, SshFxpRead.class);
076: registerMessage(SshFxpWrite.SSH_FXP_WRITE, SshFxpWrite.class);
077: registerMessage(SshFxpReadDir.SSH_FXP_READDIR,
078: SshFxpReadDir.class);
079: registerMessage(SshFxpClose.SSH_FXP_CLOSE, SshFxpClose.class);
080: registerMessage(SshFxpLStat.SSH_FXP_LSTAT, SshFxpLStat.class);
081: registerMessage(SshFxpStat.SSH_FXP_STAT, SshFxpStat.class);
082: registerMessage(SshFxpRemove.SSH_FXP_REMOVE, SshFxpRemove.class);
083: registerMessage(SshFxpRename.SSH_FXP_RENAME, SshFxpRename.class);
084: registerMessage(SshFxpRmdir.SSH_FXP_RMDIR, SshFxpRmdir.class);
085: registerMessage(SshFxpSetStat.SSH_FXP_SETSTAT,
086: SshFxpSetStat.class);
087: registerMessage(SshFxpFStat.SSH_FXP_FSTAT, SshFxpFStat.class);
088: registerMessage(SshFxpFSetStat.SSH_FXP_FSETSTAT,
089: SshFxpFSetStat.class);
090: registerMessage(SshFxpReadlink.SSH_FXP_READLINK,
091: SshFxpReadlink.class);
092: registerMessage(SshFxpSymlink.SSH_FXP_SYMLINK,
093: SshFxpSymlink.class);
094: }
095:
096: /**
097: *
098: *
099: * @param session
100: */
101: public void setSession(SessionChannelServer session) {
102: session.addEventListener(new ChannelEventListener() {
103: public void onChannelOpen(Channel channel) {
104: }
105:
106: public void onChannelEOF(Channel channel) {
107: try {
108: SftpSubsystemServer.this .session.close();
109: } catch (IOException ex) {
110: }
111: }
112:
113: public void onChannelClose(Channel channel) {
114: }
115:
116: public void onDataReceived(Channel channel, byte[] data) {
117: }
118:
119: public void onDataSent(Channel channel, byte[] data) {
120: }
121: });
122: super .setSession(session);
123: }
124:
125: /**
126: *
127: *
128: * @param msg
129: */
130: protected void onMessageReceived(SubsystemMessage msg) {
131: switch (msg.getMessageType()) {
132: case SshFxpInit.SSH_FXP_INIT: {
133: onInitialize((SshFxpInit) msg);
134:
135: break;
136: }
137:
138: case SshFxpMkdir.SSH_FXP_MKDIR: {
139: onMakeDirectory((SshFxpMkdir) msg);
140:
141: break;
142: }
143:
144: case SshFxpRealPath.SSH_FXP_REALPATH: {
145: onRealPath((SshFxpRealPath) msg);
146:
147: break;
148: }
149:
150: case SshFxpOpenDir.SSH_FXP_OPENDIR: {
151: onOpenDirectory((SshFxpOpenDir) msg);
152:
153: break;
154: }
155:
156: case SshFxpOpen.SSH_FXP_OPEN: {
157: onOpenFile((SshFxpOpen) msg);
158:
159: break;
160: }
161:
162: case SshFxpRead.SSH_FXP_READ: {
163: onReadFile((SshFxpRead) msg);
164:
165: break;
166: }
167:
168: case SshFxpWrite.SSH_FXP_WRITE: {
169: onWriteFile((SshFxpWrite) msg);
170:
171: break;
172: }
173:
174: case SshFxpReadDir.SSH_FXP_READDIR: {
175: onReadDirectory((SshFxpReadDir) msg);
176:
177: break;
178: }
179:
180: case SshFxpLStat.SSH_FXP_LSTAT: {
181: onLStat((SshFxpLStat) msg);
182:
183: break;
184: }
185:
186: case SshFxpStat.SSH_FXP_STAT: {
187: onStat((SshFxpStat) msg);
188:
189: break;
190: }
191:
192: case SshFxpFStat.SSH_FXP_FSTAT: {
193: onFStat((SshFxpFStat) msg);
194:
195: break;
196: }
197:
198: case SshFxpClose.SSH_FXP_CLOSE: {
199: onCloseFile((SshFxpClose) msg);
200:
201: break;
202: }
203:
204: case SshFxpRemove.SSH_FXP_REMOVE: {
205: onRemoveFile((SshFxpRemove) msg);
206:
207: break;
208: }
209:
210: case SshFxpRename.SSH_FXP_RENAME: {
211: onRenameFile((SshFxpRename) msg);
212:
213: break;
214: }
215:
216: case SshFxpRmdir.SSH_FXP_RMDIR: {
217: onRemoveDirectory((SshFxpRmdir) msg);
218:
219: break;
220: }
221:
222: case SshFxpSetStat.SSH_FXP_SETSTAT: {
223: onSetAttributes((SshFxpSetStat) msg);
224:
225: break;
226: }
227:
228: case SshFxpFSetStat.SSH_FXP_FSETSTAT: {
229: onSetAttributes((SshFxpFSetStat) msg);
230:
231: break;
232: }
233:
234: case SshFxpReadlink.SSH_FXP_READLINK: {
235: onReadlink((SshFxpReadlink) msg);
236:
237: break;
238: }
239:
240: case SshFxpSymlink.SSH_FXP_SYMLINK: {
241: onSymlink((SshFxpSymlink) msg);
242:
243: break;
244: }
245:
246: default: {
247: }
248: }
249: }
250:
251: private void onSetAttributes(SshFxpSetStat msg) {
252: SubsystemMessage reply;
253:
254: try {
255: nfs.setFileAttributes(checkDefaultPath(msg.getPath()), msg
256: .getAttributes());
257: reply = new SshFxpStatus(msg.getId(),
258: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
259: "The attributes were set", "");
260: } catch (FileNotFoundException fnfe) {
261: reply = new SshFxpStatus(msg.getId(),
262: new UnsignedInteger32(
263: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), fnfe
264: .getMessage(), "");
265: } catch (PermissionDeniedException pde) {
266: reply = new SshFxpStatus(msg.getId(),
267: new UnsignedInteger32(
268: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
269: pde.getMessage(), "");
270: } catch (IOException ioe) {
271: reply = new SshFxpStatus(msg.getId(),
272: new UnsignedInteger32(
273: SshFxpStatus.STATUS_FX_FAILURE), ioe
274: .getMessage(), "");
275: }
276:
277: sendMessage(reply);
278: }
279:
280: private void onSetAttributes(SshFxpFSetStat msg) {
281: SubsystemMessage reply;
282:
283: try {
284: nfs.setFileAttributes(msg.getHandle(), msg.getAttributes());
285: reply = new SshFxpStatus(msg.getId(),
286: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
287: "The attributes were set", "");
288: } catch (InvalidHandleException ihe) {
289: reply = new SshFxpStatus(msg.getId(),
290: new UnsignedInteger32(
291: SshFxpStatus.STATUS_FX_FAILURE), ihe
292: .getMessage(), "");
293: } catch (PermissionDeniedException pde) {
294: reply = new SshFxpStatus(msg.getId(),
295: new UnsignedInteger32(
296: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
297: pde.getMessage(), "");
298: } catch (IOException ioe) {
299: reply = new SshFxpStatus(msg.getId(),
300: new UnsignedInteger32(
301: SshFxpStatus.STATUS_FX_FAILURE), ioe
302: .getMessage(), "");
303: }
304:
305: sendMessage(reply);
306: }
307:
308: private void onReadlink(SshFxpReadlink msg) {
309: SubsystemMessage reply;
310:
311: try {
312: /*
313: File f = nfs.readSymbolicLink(VirtualFileSystem.translateVFSPath(
314: msg.getPath()));
315: SftpFile[] files = new SftpFile[1];
316: files[0] = new SftpFile(VirtualFileSystem.translateNFSPath(
317: f.getCanonicalPath()),
318: nfs.getFileAttributes(f.getCanonicalPath()));
319: reply = new SshFxpName(msg.getId(), files);
320: */
321: reply = new SshFxpName(msg.getId(),
322: new SftpFile[] { nfs
323: .readSymbolicLink(checkDefaultPath(msg
324: .getPath())) });
325: } catch (FileNotFoundException ioe) {
326: reply = new SshFxpStatus(msg.getId(),
327: new UnsignedInteger32(
328: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
329: .getMessage(), "");
330: } catch (PermissionDeniedException pde) {
331: reply = new SshFxpStatus(msg.getId(),
332: new UnsignedInteger32(
333: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
334: pde.getMessage(), "");
335: } catch (UnsupportedFileOperationException uso) {
336: reply = new SshFxpStatus(msg.getId(),
337: new UnsignedInteger32(
338: SshFxpStatus.STATUS_FX_OP_UNSUPPORTED), uso
339: .getMessage(), "");
340: } catch (IOException ioe2) {
341: reply = new SshFxpStatus(msg.getId(),
342: new UnsignedInteger32(
343: SshFxpStatus.STATUS_FX_FAILURE), ioe2
344: .getMessage(), "");
345: }
346:
347: sendMessage(reply);
348: }
349:
350: private void onSymlink(SshFxpSymlink msg) {
351: SubsystemMessage reply;
352:
353: try {
354: /*
355: nfs.createSymbolicLink(VirtualFileSystem.translateVFSPath(
356: msg.getLinkPath()),
357: VirtualFileSystem.translateVFSPath(msg.getTargetPath()));
358: */
359: nfs.createSymbolicLink(checkDefaultPath(msg.getLinkPath()),
360: checkDefaultPath(msg.getTargetPath()));
361: reply = new SshFxpStatus(msg.getId(),
362: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
363: "The symbolic link was created", "");
364: } catch (FileNotFoundException ioe) {
365: reply = new SshFxpStatus(msg.getId(),
366: new UnsignedInteger32(
367: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
368: .getMessage(), "");
369: } catch (PermissionDeniedException pde) {
370: reply = new SshFxpStatus(msg.getId(),
371: new UnsignedInteger32(
372: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
373: pde.getMessage(), "");
374: } catch (IOException ioe2) {
375: reply = new SshFxpStatus(msg.getId(),
376: new UnsignedInteger32(
377: SshFxpStatus.STATUS_FX_FAILURE), ioe2
378: .getMessage(), "");
379: } catch (UnsupportedFileOperationException uso) {
380: reply = new SshFxpStatus(msg.getId(),
381: new UnsignedInteger32(
382: SshFxpStatus.STATUS_FX_OP_UNSUPPORTED), uso
383: .getMessage(), "");
384: }
385:
386: sendMessage(reply);
387: }
388:
389: private void onRemoveDirectory(SshFxpRmdir msg) {
390: SubsystemMessage reply;
391:
392: try {
393: /*
394: nfs.removeDirectory(VirtualFileSystem.translateVFSPath(
395: msg.getPath()));
396: */
397: nfs.removeDirectory(checkDefaultPath(msg.getPath()));
398: reply = new SshFxpStatus(msg.getId(),
399: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
400: "The directory was removed", "");
401: } catch (FileNotFoundException ioe) {
402: reply = new SshFxpStatus(msg.getId(),
403: new UnsignedInteger32(
404: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
405: .getMessage(), "");
406: } catch (IOException ioe2) {
407: reply = new SshFxpStatus(msg.getId(),
408: new UnsignedInteger32(
409: SshFxpStatus.STATUS_FX_FAILURE), ioe2
410: .getMessage(), "");
411: } catch (PermissionDeniedException pde) {
412: reply = new SshFxpStatus(msg.getId(),
413: new UnsignedInteger32(
414: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
415: pde.getMessage(), "");
416: }
417:
418: sendMessage(reply);
419: }
420:
421: private void onRenameFile(SshFxpRename msg) {
422: SubsystemMessage reply;
423:
424: try {
425: /*
426: nfs.renameFile(VirtualFileSystem.translateVFSPath(msg.getOldPath()),
427: VirtualFileSystem.translateVFSPath(msg.getNewPath()));
428: */
429: nfs.renameFile(checkDefaultPath(msg.getOldPath()),
430: checkDefaultPath(msg.getNewPath()));
431: reply = new SshFxpStatus(msg.getId(),
432: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
433: "The file was removed", "");
434: } catch (FileNotFoundException ioe) {
435: reply = new SshFxpStatus(msg.getId(),
436: new UnsignedInteger32(
437: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
438: .getMessage(), "");
439: } catch (IOException ioe2) {
440: reply = new SshFxpStatus(msg.getId(),
441: new UnsignedInteger32(
442: SshFxpStatus.STATUS_FX_FAILURE), ioe2
443: .getMessage(), "");
444: } catch (PermissionDeniedException pde) {
445: reply = new SshFxpStatus(msg.getId(),
446: new UnsignedInteger32(
447: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
448: pde.getMessage(), "");
449: }
450:
451: sendMessage(reply);
452: }
453:
454: private void onRemoveFile(SshFxpRemove msg) {
455: SubsystemMessage reply;
456:
457: try {
458: /*
459: nfs.removeFile(VirtualFileSystem.translateVFSPath(msg.getFilename()));
460: */
461: nfs.removeFile(checkDefaultPath(msg.getFilename()));
462: reply = new SshFxpStatus(msg.getId(),
463: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
464: "The file was removed", "");
465: } catch (FileNotFoundException ioe) {
466: reply = new SshFxpStatus(msg.getId(),
467: new UnsignedInteger32(
468: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
469: .getMessage(), "");
470: } catch (IOException ioe2) {
471: reply = new SshFxpStatus(msg.getId(),
472: new UnsignedInteger32(
473: SshFxpStatus.STATUS_FX_FAILURE), ioe2
474: .getMessage(), "");
475: } catch (PermissionDeniedException pde) {
476: reply = new SshFxpStatus(msg.getId(),
477: new UnsignedInteger32(
478: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
479: pde.getMessage(), "");
480: }
481:
482: sendMessage(reply);
483: }
484:
485: private void onOpenFile(SshFxpOpen msg) {
486: SubsystemMessage reply;
487:
488: try {
489: reply = new SshFxpHandle(msg.getId(), nfs.openFile(
490: checkDefaultPath(msg.getFilename()), msg
491: .getPflags(), msg.getAttributes()));
492: } catch (FileNotFoundException ioe) {
493: reply = new SshFxpStatus(msg.getId(),
494: new UnsignedInteger32(
495: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
496: .getMessage(), "");
497: } catch (IOException ioe2) {
498: reply = new SshFxpStatus(msg.getId(),
499: new UnsignedInteger32(
500: SshFxpStatus.STATUS_FX_FAILURE), ioe2
501: .getMessage(), "");
502: } catch (PermissionDeniedException pde) {
503: reply = new SshFxpStatus(msg.getId(),
504: new UnsignedInteger32(
505: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
506: pde.getMessage(), "");
507: }
508:
509: sendMessage(reply);
510: }
511:
512: private void onReadFile(SshFxpRead msg) {
513: SubsystemMessage reply;
514:
515: try {
516: reply = new SshFxpData(msg.getId(), nfs.readFile(msg
517: .getHandle(), msg.getOffset(), msg.getLength()));
518: } catch (EOFException eof) {
519: reply = new SshFxpStatus(msg.getId(),
520: new UnsignedInteger32(SshFxpStatus.STATUS_FX_EOF),
521: eof.getMessage(), "");
522: } catch (InvalidHandleException ihe) {
523: reply = new SshFxpStatus(msg.getId(),
524: new UnsignedInteger32(
525: SshFxpStatus.STATUS_FX_FAILURE), ihe
526: .getMessage(), "");
527: } catch (IOException ioe2) {
528: reply = new SshFxpStatus(msg.getId(),
529: new UnsignedInteger32(
530: SshFxpStatus.STATUS_FX_FAILURE), ioe2
531: .getMessage(), "");
532: }
533:
534: sendMessage(reply);
535: }
536:
537: private void onWriteFile(SshFxpWrite msg) {
538: SubsystemMessage reply;
539:
540: try {
541: nfs.writeFile(msg.getHandle(), msg.getOffset(), msg
542: .getData(), 0, msg.getData().length);
543: reply = new SshFxpStatus(msg.getId(),
544: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
545: "The write completed successfully", "");
546: } catch (InvalidHandleException ihe) {
547: reply = new SshFxpStatus(msg.getId(),
548: new UnsignedInteger32(
549: SshFxpStatus.STATUS_FX_FAILURE), ihe
550: .getMessage(), "");
551: } catch (IOException ioe2) {
552: reply = new SshFxpStatus(msg.getId(),
553: new UnsignedInteger32(
554: SshFxpStatus.STATUS_FX_FAILURE), ioe2
555: .getMessage(), "");
556: }
557:
558: sendMessage(reply);
559: }
560:
561: private void onCloseFile(SshFxpClose msg) {
562: SubsystemMessage reply;
563:
564: try {
565: nfs.closeFile(msg.getHandle());
566: reply = new SshFxpStatus(msg.getId(),
567: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
568: "The operation completed", "");
569: } catch (InvalidHandleException ihe) {
570: reply = new SshFxpStatus(msg.getId(),
571: new UnsignedInteger32(
572: SshFxpStatus.STATUS_FX_FAILURE), ihe
573: .getMessage(), "");
574: } catch (IOException ioe2) {
575: reply = new SshFxpStatus(msg.getId(),
576: new UnsignedInteger32(
577: SshFxpStatus.STATUS_FX_FAILURE), ioe2
578: .getMessage(), "");
579: }
580:
581: sendMessage(reply);
582: }
583:
584: private void onFStat(SshFxpFStat msg) {
585: SubsystemMessage reply;
586:
587: try {
588: reply = new SshFxpAttrs(msg.getId(), nfs
589: .getFileAttributes(msg.getHandle()));
590: } catch (InvalidHandleException ihe) {
591: reply = new SshFxpStatus(msg.getId(),
592: new UnsignedInteger32(
593: SshFxpStatus.STATUS_FX_FAILURE), ihe
594: .getMessage(), "");
595: } catch (IOException ioe2) {
596: reply = new SshFxpStatus(msg.getId(),
597: new UnsignedInteger32(
598: SshFxpStatus.STATUS_FX_FAILURE), ioe2
599: .getMessage(), "");
600: }
601:
602: sendMessage(reply);
603: }
604:
605: private void onStat(SshFxpStat msg) {
606: SubsystemMessage reply;
607:
608: try {
609: String path = checkDefaultPath(msg.getPath());
610:
611: if (nfs.fileExists(path)) {
612: SftpFile[] files = new SftpFile[1];
613: reply = new SshFxpAttrs(msg.getId(), nfs
614: .getFileAttributes(
615: /*nfs.getCanonicalPath(*/
616: msg.getPath() /*)*/));
617: } else {
618: reply = new SshFxpStatus(msg.getId(),
619: new UnsignedInteger32(
620: SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
621: path + " is not a valid file path", "");
622: }
623: } catch (FileNotFoundException ioe) {
624: reply = new SshFxpStatus(msg.getId(),
625: new UnsignedInteger32(
626: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
627: .getMessage(), "");
628: } catch (IOException ioe2) {
629: reply = new SshFxpStatus(msg.getId(),
630: new UnsignedInteger32(
631: SshFxpStatus.STATUS_FX_FAILURE), ioe2
632: .getMessage(), "");
633: }
634:
635: sendMessage(reply);
636: }
637:
638: private void onLStat(SshFxpLStat msg) {
639: SubsystemMessage reply;
640:
641: try {
642: String path = checkDefaultPath(msg.getPath());
643:
644: if (nfs.fileExists(path)) {
645: SftpFile[] files = new SftpFile[1];
646: reply = new SshFxpAttrs(msg.getId(), nfs
647: .getFileAttributes(nfs.getCanonicalPath(msg
648: .getPath())));
649: } else {
650: reply = new SshFxpStatus(msg.getId(),
651: new UnsignedInteger32(
652: SshFxpStatus.STATUS_FX_NO_SUCH_FILE),
653: path + " is not a valid file path", "");
654: }
655: } catch (FileNotFoundException ioe) {
656: reply = new SshFxpStatus(msg.getId(),
657: new UnsignedInteger32(
658: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
659: .getMessage(), "");
660: } catch (IOException ioe2) {
661: reply = new SshFxpStatus(msg.getId(),
662: new UnsignedInteger32(
663: SshFxpStatus.STATUS_FX_FAILURE), ioe2
664: .getMessage(), "");
665: }
666:
667: sendMessage(reply);
668: }
669:
670: private void onReadDirectory(SshFxpReadDir msg) {
671: SubsystemMessage reply;
672:
673: try {
674: /*
675: File[] files = nfs.readDirectory(msg.getHandle());
676: SftpFile[] sftpfiles = new SftpFile[files.length];
677: for (int i = 0; i < files.length; i++) {
678: sftpfiles[i] = new SftpFile(files[i].getName(),
679: nfs.getFileAttributes(files[i].getCanonicalPath()));
680: }
681: */
682: SftpFile[] sftpfiles = nfs.readDirectory(msg.getHandle());
683: reply = new SshFxpName(msg.getId(), sftpfiles);
684: } catch (FileNotFoundException ioe) {
685: reply = new SshFxpStatus(msg.getId(),
686: new UnsignedInteger32(
687: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
688: .getMessage(), "");
689: } catch (InvalidHandleException ihe) {
690: reply = new SshFxpStatus(msg.getId(),
691: new UnsignedInteger32(
692: SshFxpStatus.STATUS_FX_FAILURE), ihe
693: .getMessage(), "");
694: } catch (EOFException eof) {
695: reply = new SshFxpStatus(msg.getId(),
696: new UnsignedInteger32(SshFxpStatus.STATUS_FX_EOF),
697: eof.getMessage(), "");
698: } catch (IOException ioe2) {
699: reply = new SshFxpStatus(msg.getId(),
700: new UnsignedInteger32(
701: SshFxpStatus.STATUS_FX_FAILURE), ioe2
702: .getMessage(), "");
703: }
704:
705: sendMessage(reply);
706: }
707:
708: private void onOpenDirectory(SshFxpOpenDir msg) {
709: SubsystemMessage reply;
710:
711: try {
712: /*
713: String path = VirtualFileSystem.translateVFSPath(msg.getPath());
714: */
715: String path = checkDefaultPath(msg.getPath());
716: reply = new SshFxpHandle(msg.getId(), nfs
717: .openDirectory(path));
718: } catch (FileNotFoundException ioe) {
719: reply = new SshFxpStatus(msg.getId(),
720: new UnsignedInteger32(
721: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
722: .getMessage(), "");
723: } catch (IOException ioe2) {
724: reply = new SshFxpStatus(msg.getId(),
725: new UnsignedInteger32(
726: SshFxpStatus.STATUS_FX_FAILURE), ioe2
727: .getMessage(), "");
728: } catch (PermissionDeniedException pde) {
729: reply = new SshFxpStatus(msg.getId(),
730: new UnsignedInteger32(
731: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
732: pde.getMessage(), "");
733: }
734:
735: sendMessage(reply);
736: }
737:
738: private void onRealPath(SshFxpRealPath msg) {
739: SubsystemMessage reply;
740:
741: try {
742: /*
743: String path = VirtualFileSystem.translateVFSPath(msg.getPath());
744: path = VirtualFileSystem.translateNFSPath(path);
745: */
746: String path = nfs.getRealPath(checkDefaultPath(msg
747: .getPath()));
748:
749: if (path != null) {
750: SftpFile[] files = new SftpFile[1];
751: files[0] = new SftpFile(path);
752: reply = new SshFxpName(msg.getId(), files);
753: } else {
754: reply = new SshFxpStatus(
755: msg.getId(),
756: new UnsignedInteger32(
757: SshFxpStatus.STATUS_FX_FAILURE),
758: msg.getPath()
759: + " could not be translated into a system dependent path",
760: "");
761: }
762: } catch (FileNotFoundException ioe) {
763: reply = new SshFxpStatus(msg.getId(),
764: new UnsignedInteger32(
765: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
766: .getMessage(), "");
767: } catch (IOException ioe) {
768: reply = new SshFxpStatus(msg.getId(),
769: new UnsignedInteger32(
770: SshFxpStatus.STATUS_FX_FAILURE), ioe
771: .getMessage(), "");
772: }
773:
774: sendMessage(reply);
775: }
776:
777: private void onMakeDirectory(SshFxpMkdir msg) {
778: SubsystemMessage reply;
779:
780: try {
781: /*
782: String path = VirtualFileSystem.translateVFSPath(msg.getPath());
783: */
784: String path = checkDefaultPath(msg.getPath());
785:
786: if (nfs.makeDirectory(path)) {
787: reply = new SshFxpStatus(
788: msg.getId(),
789: new UnsignedInteger32(SshFxpStatus.STATUS_FX_OK),
790: "The operation completed sucessfully", "");
791: } else {
792: // Send an error back to the client
793: reply = new SshFxpStatus(msg.getId(),
794: new UnsignedInteger32(
795: SshFxpStatus.STATUS_FX_FAILURE),
796: "The operation failed", "");
797: }
798: } catch (FileNotFoundException ioe) {
799: reply = new SshFxpStatus(msg.getId(),
800: new UnsignedInteger32(
801: SshFxpStatus.STATUS_FX_NO_SUCH_FILE), ioe
802: .getMessage(), "");
803: } catch (PermissionDeniedException pde) {
804: reply = new SshFxpStatus(msg.getId(),
805: new UnsignedInteger32(
806: SshFxpStatus.STATUS_FX_PERMISSION_DENIED),
807: pde.getMessage(), "");
808: } catch (IOException ioe) {
809: reply = new SshFxpStatus(msg.getId(),
810: new UnsignedInteger32(
811: SshFxpStatus.STATUS_FX_FAILURE), ioe
812: .getMessage(), "");
813: }
814:
815: sendMessage(reply);
816: }
817:
818: private String checkDefaultPath(String path) throws IOException {
819: // Use the users home directory if no path is supplied
820: if (path.equals("")) {
821: return nfs.getDefaultPath(SshThread.getCurrentThreadUser());
822: } else {
823: return path;
824: }
825: }
826:
827: private void onInitialize(SshFxpInit msg) {
828: // Get the native file system
829: nfs = NativeFileSystemProvider.getInstance();
830:
831: // Determine the users home directory
832: if (msg.getVersion().intValue() == VERSION_3) {
833: SshFxpVersion reply = new SshFxpVersion(
834: new UnsignedInteger32(VERSION_3), null);
835: sendMessage(reply);
836: } else {
837: // Wrong version
838: }
839: }
840: }
|