Examples of SshFile


Examples of org.apache.sshd.common.file.SshFile

        String name = header.substring(header.indexOf(' ', 6) + 1);

        if (length != 0) {
            throw new IOException("Expected 0 length for directory but got " + length);
        }
        SshFile file;
        if (path.doesExist() && path.isDirectory()) {
            file = root.getFile(path, name);
        } else if (!path.doesExist() && path.getParentFile().doesExist() && path.getParentFile().isDirectory()) {
            file = path;
        } else {
            throw new IOException("Can not write to " + path);
        }
        if (!(file.doesExist() && file.isDirectory()) && !file.mkdir()) {
            throw new IOException("Could not create directory " + file);
        }

        if (preserve) {
            Map<SshFile.Attribute, Object> attrs = new HashMap<SshFile.Attribute, Object>();
            attrs.put(SshFile.Attribute.Permissions, fromOctalPerms(perms));
            if (time != null) {
                attrs.put(SshFile.Attribute.LastModifiedTime, time[0]);
                attrs.put(SshFile.Attribute.LastAccessTime, time[1]);
            }
            file.setAttributes(attrs);
        }

        ack();

        time = null;
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

        String perms = header.substring(1, 5);
        long length = Long.parseLong(header.substring(6, header.indexOf(' ', 6)));
        String name = header.substring(header.indexOf(' ', 6) + 1);

        SshFile file;
        if (path.doesExist() && path.isDirectory()) {
            file = root.getFile(path, name);
        } else if (path.doesExist() && path.isFile()) {
            file = path;
        } else if (!path.doesExist() && path.getParentFile().doesExist() && path.getParentFile().isDirectory()) {
            file = path;
        } else {
            throw new IOException("Can not write to " + path);
        }
        if (file.doesExist() && file.isDirectory()) {
            throw new IOException("File is a directory: " + file);
        } else if (file.doesExist() && !file.isWritable()) {
            throw new IOException("Can not write to file: " + file);
        }
        OutputStream os = file.createOutputStream(0);
        try {
            ack();

            byte[] buffer = new byte[8192];
            while (length > 0) {
                int len = (int) Math.min(length, buffer.length);
                len = in.read(buffer, 0, len);
                if (len <= 0) {
                    throw new IOException("End of stream reached");
                }
                os.write(buffer, 0, len);
                length -= len;
            }
        } finally {
            os.close();
        }

        if (preserve) {
            Map<SshFile.Attribute, Object> attrs = new HashMap<SshFile.Attribute, Object>();
            attrs.put(SshFile.Attribute.Permissions, fromOctalPerms(perms));
            if (time != null) {
                attrs.put(SshFile.Attribute.LastModifiedTime, time[0]);
                attrs.put(SshFile.Attribute.LastAccessTime, time[1]);
            }
            file.setAttributes(attrs);
        }

        ack();
        readAck(false);
    }
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

                String path = buffer.getString();
                int pflags = buffer.getInt();
                Map<SshFile.Attribute, Object> attrs = readAttrs(buffer);
                log.debug("Received SSH_FXP_OPEN (path={}, pflags={}, attrs={})", new Object[] { path, pflags, attrs });
                try {
                    SshFile file = resolveFile(path);
                    if (file.doesExist()) {
                        if (((pflags & SSH_FXF_CREAT) != 0) && ((pflags & SSH_FXF_EXCL) != 0)) {
                            sendStatus(id, SSH_FX_FAILURE, path);
                            return;
                        }
                    } else {
                        if (((pflags & SSH_FXF_CREAT) != 0)) {
                            if (!file.isWritable()) {
                                sendStatus(id, SSH_FX_PERMISSION_DENIED, "Can not create " + path);
                                return;
                            }
                            file.create();
                        }
                    }
                    String acc = ((pflags & (SSH_FXF_READ | SSH_FXF_WRITE)) != 0 ? "r" : "") +
                            ((pflags & SSH_FXF_WRITE) != 0 ? "w" : "");
                    if ((pflags & SSH_FXF_TRUNC) != 0) {
                        file.truncate();
                    }
                    if ((pflags & SSH_FXF_CREAT) != 0) {
                        file.setAttributes(attrs);
                    }
                    String handle = UUID.randomUUID().toString();
                    handles.put(handle, new FileHandle(file));
                    sendHandle(id, handle);
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage() == null ? "" : e.getMessage());
                }
                break;
            }
            case SSH_FXP_CLOSE: {
                String handle = buffer.getString();
                log.debug("Received SSH_FXP_CLOSE (handle={})", handle);
                try {
                    Handle h = handles.get(handle);
                    if (h == null) {
                        sendStatus(id, SSH_FX_FAILURE, handle, "");
                    } else {
                        handles.remove(handle);
                        h.close();
                        sendStatus(id, SSH_FX_OK, "", "");
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_READ: {
                String handle = buffer.getString();
                long offset = buffer.getLong();
                int len = buffer.getInt();
                log.debug("Received SSH_FXP_READ (handle={}, offset={}, length={})", new Object[] { handle, offset, len });
                try {
                    Handle p = handles.get(handle);
                    if (!(p instanceof FileHandle)) {
                        sendStatus(id, SSH_FX_FAILURE, handle);
                    } else {
                        FileHandle fh = (FileHandle) p;
                        byte[] b = new byte[Math.min(len, Buffer.MAX_LEN)];
                        len = fh.read(b, offset);
                        if (len >= 0) {
                            Buffer buf = new Buffer(len + 5);
                            buf.putByte((byte) SSH_FXP_DATA);
                            buf.putInt(id);
                            buf.putBytes(b, 0, len);
                            send(buf);
                        } else {
                            sendStatus(id, SSH_FX_EOF, "");
                        }
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_WRITE: {
                String handle = buffer.getString();
                long offset = buffer.getLong();
                byte[] data = buffer.getBytes();
                log.debug("Received SSH_FXP_WRITE (handle={}, offset={}, data=byte[{}])", new Object[] { handle, offset, data.length });
                try {
                    Handle p = handles.get(handle);
                    if (!(p instanceof FileHandle)) {
                        sendStatus(id, SSH_FX_FAILURE, handle);
                    } else {
                        FileHandle fh = (FileHandle) p;
                        fh.write(data, offset);
                        SshFile sshFile = fh.getFile();

                        sshFile.setLastModified(new Date().getTime());
                       
                        sendStatus(id, SSH_FX_OK, "");
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_LSTAT: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_LSTAT (path={})", path);
                try {
                    SshFile p = resolveFile(path);
                    sendAttrs(id, p, false);
                } catch (FileNotFoundException e) {
                    sendStatus(id, SSH_FX_NO_SUCH_FILE, e.getMessage());
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_FSTAT: {
                String handle = buffer.getString();
                log.debug("Received SSH_FXP_FSTAT (handle={})", handle);
                try {
                    Handle p = handles.get(handle);
                    if (p == null) {
                        sendStatus(id, SSH_FX_FAILURE, handle);
                    } else {
                        sendAttrs(id, p.getFile(), true);
                    }
                } catch (FileNotFoundException e) {
                    sendStatus(id, SSH_FX_NO_SUCH_FILE, e.getMessage());
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_SETSTAT: {
                String path = buffer.getString();
                Map<SshFile.Attribute, Object> attrs = readAttrs(buffer);
                log.debug("Received SSH_FXP_SETSTAT (path={}, attrs={})", path, attrs);
                try {
                    SshFile p = resolveFile(path);
                    p.setAttributes(attrs);
                    sendStatus(id, SSH_FX_OK, "");
                } catch (FileNotFoundException e) {
                    sendStatus(id, SSH_FX_NO_SUCH_FILE, e.getMessage());
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                } catch (UnsupportedOperationException e) {
                    sendStatus(id, SSH_FX_FAILURE, "");
                }
                break;
            }
            case SSH_FXP_FSETSTAT: {
                String handle = buffer.getString();
                Map<SshFile.Attribute, Object> attrs = readAttrs(buffer);
                log.debug("Received SSH_FXP_FSETSTAT (handle={}, attrs={})", handle, attrs);
                try {
                    Handle p = handles.get(handle);
                    if (p == null) {
                        sendStatus(id, SSH_FX_FAILURE, handle);
                    } else {
                        p.getFile().setAttributes(attrs);
                        sendStatus(id, SSH_FX_OK, "");
                    }
                } catch (FileNotFoundException e) {
                    sendStatus(id, SSH_FX_NO_SUCH_FILE, e.getMessage());
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                } catch (UnsupportedOperationException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_OPENDIR: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_OPENDIR (path={})", path);
                try {
                    SshFile p = resolveFile(path);
                    if (!p.doesExist()) {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, path);
                    } else if (!p.isDirectory()) {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, path);
                    } else if (!p.isReadable()) {
                        sendStatus(id, SSH_FX_PERMISSION_DENIED, path);
                    } else {
                        String handle = UUID.randomUUID().toString();
                        handles.put(handle, new DirectoryHandle(p));
                        sendHandle(id, handle);
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_READDIR: {
                String handle = buffer.getString();
                log.debug("Received SSH_FXP_READDIR (handle={})", handle);
                try {
                    Handle p = handles.get(handle);
                    if (!(p instanceof DirectoryHandle)) {
                        sendStatus(id, SSH_FX_FAILURE, handle);
                    } else if (((DirectoryHandle) p).isDone()) {
                        sendStatus(id, SSH_FX_EOF, "", "");
                    } else if (!p.getFile().doesExist()) {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, p.getFile().getAbsolutePath());
                    } else if (!p.getFile().isDirectory()) {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, p.getFile().getAbsolutePath());
                    } else if (!p.getFile().isReadable()) {
                        sendStatus(id, SSH_FX_PERMISSION_DENIED, p.getFile().getAbsolutePath());
                    } else {
                        DirectoryHandle dh = (DirectoryHandle) p;
                        if (dh.hasNext()) {
                            // There is at least one file in the directory.
                            // Send only a few files at a time to not create packets of a too
                            // large size or have a timeout to occur.
                            sendName(id, dh);
                            if (!dh.hasNext()) {
                                // if no more files to send
                                dh.setDone(true);
                                dh.clearFileList();
                            }
                        } else {
                            // empty directory
                            dh.setDone(true);
                            dh.clearFileList();
                            sendStatus(id, SSH_FX_EOF, "", "");
                        }
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_REMOVE: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_REMOVE (path={})", path);
                try {
                    SshFile p = resolveFile(path);
                    if (!p.doesExist()) {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, p.getAbsolutePath());
                    } else if (p.isDirectory()) {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, p.getAbsolutePath());
                    } else if (!p.delete()) {
                        sendStatus(id, SSH_FX_FAILURE, "Failed to delete file");
                    } else {
                        sendStatus(id, SSH_FX_OK, "");
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_MKDIR: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_MKDIR (path={})", path);
                // attrs
                try {
                    SshFile p = resolveFile(path);
                    if (p.doesExist()) {
                        if (p.isDirectory()) {
                            sendStatus(id, SSH_FX_FAILURE, p.getAbsolutePath());
                        } else {
                            sendStatus(id, SSH_FX_NO_SUCH_FILE, p.getAbsolutePath());
                        }
                    } else if (!p.isWritable()) {
                        sendStatus(id, SSH_FX_PERMISSION_DENIED, p.getAbsolutePath());
                    } else if (!p.mkdir()) {
                        throw new IOException("Error creating dir " + path);
                    } else {
                        sendStatus(id, SSH_FX_OK, "");
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_RMDIR: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_RMDIR (path={})", path);
                // attrs
                try {
                    SshFile p = resolveFile(path);
                    if (p.isDirectory()) {
                        if (p.doesExist()) {
                            if (p.listSshFiles().size() == 0) {
                                if (p.delete()) {
                                    sendStatus(id, SSH_FX_OK, "");
                                } else {
                                    sendStatus(id, SSH_FX_FAILURE, "Unable to delete directory " + path);
                                }
                            } else {
                                sendStatus(id, SSH_FX_FAILURE, path);
                            }
                        } else {
                            sendStatus(id, SSH_FX_NO_SUCH_FILE, path);
                        }
                    } else {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, p.getAbsolutePath());
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_REALPATH: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_REALPATH (path={})", path);
                if (path.trim().length() == 0) {
                    path = ".";
                }
                try {
                    SshFile p = resolveFile(path);
                    sendPath(id, p);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    sendStatus(id, SSH_FX_NO_SUCH_FILE, e.getMessage());
                } catch (IOException e) {
                    e.printStackTrace();
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_STAT: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_STAT (path={})", path);
                try {
                    SshFile p = resolveFile(path);
                    sendAttrs(id, p, true);
                } catch (FileNotFoundException e) {
                    sendStatus(id, SSH_FX_NO_SUCH_FILE, e.getMessage());
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_RENAME: {
                String oldPath = buffer.getString();
                String newPath = buffer.getString();
                log.debug("Received SSH_FXP_RENAME (oldPath={}, newPath={})", oldPath, newPath);
                try {
                    SshFile o = resolveFile(oldPath);
                    SshFile n = resolveFile(newPath);
                    if (!o.doesExist()) {
                        sendStatus(id, SSH_FX_NO_SUCH_FILE, o.getAbsolutePath());
                    } else if (n.doesExist()) {
                        sendStatus(id, SSH_FX_FAILURE, n.getAbsolutePath());
                    } else if (!o.move(n)) {
                        sendStatus(id, SSH_FX_FAILURE, "Failed to rename file");
                    } else {
                        sendStatus(id, SSH_FX_OK, "");
                    }
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
                }
                break;
            }
            case SSH_FXP_READLINK: {
                String path = buffer.getString();
                log.debug("Received SSH_FXP_READLINK (path={})", path);
                try {
                    SshFile f = resolveFile(path);
                    String l = f.readSymbolicLink();
                    sendLink(id, l);
                } catch (UnsupportedOperationException e) {
                    sendStatus(id, SSH_FX_OP_UNSUPPORTED, "Command " + type + " is unsupported or not implemented");
                } catch (IOException e) {
                    sendStatus(id, SSH_FX_FAILURE, e.getMessage());
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

                    basedir = pattern.substring(0, lastSep);
                    pattern = pattern.substring(lastSep + 1);
                }
                String[] included = new DirectoryScanner(basedir, pattern).scan();
                for (String path : included) {
                    SshFile file = root.getFile(basedir + "/" + path);
                    if (file.isFile()) {
                        sendFile(file, preserve);
                    } else if (file.isDirectory()) {
                        if (!recursive) {
                            out.write(ScpHelper.WARNING);
                            out.write((path + " not a regular file\n").getBytes());
                        } else {
                            sendDir(file, preserve);
                        }
                    } else {
                        out.write(ScpHelper.WARNING);
                        out.write((path + " unknown file type\n").getBytes());
                    }
                }
            } else {
                String basedir = "";
                int lastSep = pattern.lastIndexOf('/');
                if (lastSep >= 0) {
                    basedir = pattern.substring(0, lastSep);
                    pattern = pattern.substring(lastSep + 1);
                }
                SshFile file = root.getFile(basedir + "/" + pattern);
                if (!file.doesExist()) {
                    throw new IOException(file + ": no such file or directory");
                }
                if (file.isFile()) {
                    sendFile(file, preserve);
                } else if (file.isDirectory()) {
                    if (!recursive) {
                        throw new IOException(file + " not a regular file");
                    } else {
                        sendDir(file, preserve);
                    }
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

        buffer.putInt(id);
        int wpos = buffer.wpos();
        buffer.putInt(0);
        int nb = 0;
        while (files.hasNext() && buffer.wpos() < MAX_PACKET_LENGTH) {
            SshFile f = files.next();
            buffer.putString(f.getName());
            buffer.putString(getLongName(f)); // Format specified in the specs
            writeAttrs(buffer, f, false);
            nb++;
        }
        int oldpos = buffer.wpos();
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

        public boolean hasNext() {
            return fileIndex < fileList.size();
        }

        public SshFile next() {
            SshFile f = fileList.get(fileIndex);
            fileIndex++;
            return f;
        }
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

    public boolean hasNext() {
        return fileIndex < fileList.size();
    }

    public SshFile next() {
        SshFile f = fileList.get(fileIndex);
        fileIndex++;
        return f;
    }
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

        assertEquals("V:/temp", view.getFile("V:/../../temp").getAbsolutePath());
        assertEquals(new File("A:/bar/temp").toString(), ((NativeSshFile) view.getFile("V:/../../temp")).getNativeFile().toString());

        assertEquals("X:/", view.getFile("..").getAbsolutePath());

        SshFile cur = view.getFile(".");
        assertEquals("X:/git", cur.getAbsolutePath());
        cur = view.getFile(cur, "..");
        assertEquals("X:/", cur.getAbsolutePath());


    }
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

    private Reply doProcessRename(SshFxpRenameRequest request) throws IOException {
        int id = request.getId();
        String oldPath = request.getOldPath();
        String newPath = request.getNewPath();
        SshFile o = resolveFile(oldPath);
        SshFile n = resolveFile(newPath);
        if (!o.doesExist()) {
            return new SshFxpStatusReply(id, SSH_FX_NO_SUCH_FILE, o.getAbsolutePath());
        } else if (n.doesExist()) {
            return new SshFxpStatusReply(id, SSH_FX_FILE_ALREADY_EXISTS, n.getAbsolutePath());
        } else if (!o.move(n)) {
            return new SshFxpStatusReply(id, SSH_FX_FAILURE, "Failed to rename file");
        } else {
            return new SshFxpStatusReply(id, SSH_FX_OK, "");
        }
View Full Code Here

Examples of org.apache.sshd.common.file.SshFile

        int id = request.getId();
        String path = request.getPath();
        if (path.trim().length() == 0) {
            path = ".";
        }
        SshFile p = resolveFile(path);
        for (String s : request.getCompose()) {
            p = this.root.getFile(p, s);
        }
        String normalizedPath = SelectorUtils.normalizePath(p.getAbsolutePath(), "/");
        if (normalizedPath.length() == 0) {
            normalizedPath = "/";
        }
        p = resolveFile(normalizedPath);
        if (p.getName().length() == 0) {
            p = resolveFile(".");
        }
        boolean exists = (request.getOptions() != SSH_FXP_REALPATH_NO_CHECK) && p.doesExist();
        if (!exists && request.getOptions() == SSH_FXP_REALPATH_STAT_ALWAYS) {
            return new SshFxpStatusReply(id, SSH_FX_NO_SUCH_FILE, p.getAbsolutePath());
        } else if (exists && (request.getOptions() == SSH_FXP_REALPATH_STAT_IF || request.getOptions() == SSH_FXP_REALPATH_STAT_ALWAYS)) {
            SshFxpNameReply reply = new SshFxpNameReply(id);
            int flags = SSH_FILEXFER_ATTR_PERMISSIONS | SSH_FILEXFER_ATTR_SIZE;
            reply.addFile(p, normalizedPath, getLongName(p), new FileAttributes(p, flags));
            return reply;
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.