Search in sources :

Example 1 with SFTPv3FileAttributes

use of com.trilead.ssh2.SFTPv3FileAttributes in project intellij-community by JetBrains.

the class SFTPv3Client method fsetstat.

/**
	 * 	Modify the attributes of a file. Used for operations such as changing
	 *  the ownership, permissions or access times, as well as for truncating a file.
	 * 
	 * @param handle a SFTPv3FileHandle handle
	 * @param attr A SFTPv3FileAttributes object. Specifies the modifications to be
	 *             made to the attributes of the file. Empty fields will be ignored.
	 * @throws IOException
	 */
public void fsetstat(SFTPv3FileHandle handle, SFTPv3FileAttributes attr) throws IOException {
    checkHandleValidAndOpen(handle);
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
    tw.writeBytes(createAttrs(attr));
    if (debug != null) {
        debug.println("Sending SSH_FXP_FSETSTAT...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_FSETSTAT, req_id, tw.getBytes());
    expectStatusOKMessage(req_id);
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 2 with SFTPv3FileAttributes

use of com.trilead.ssh2.SFTPv3FileAttributes in project hudson-2.x by hudson.

the class SFTPClient method mkdirs.

/**
 * Makes sure that the directory exists, by creating it if necessary.
 */
public void mkdirs(String path, int posixPermission) throws IOException {
    SFTPv3FileAttributes atts = _stat(path);
    if (atts != null && atts.isDirectory())
        return;
    int idx = path.lastIndexOf("/");
    if (idx > 0)
        mkdirs(path.substring(0, idx), posixPermission);
    try {
        mkdir(path, posixPermission);
    } catch (IOException e) {
        throw new IOException2("Failed to mkdir " + path, e);
    }
}
Also used : IOException(java.io.IOException) SFTPv3FileAttributes(com.trilead.ssh2.SFTPv3FileAttributes) IOException2(hudson.util.IOException2)

Example 3 with SFTPv3FileAttributes

use of com.trilead.ssh2.SFTPv3FileAttributes in project Payara by payara.

the class LogFilterForInstance method downloadGivenInstanceLogFile.

public File downloadGivenInstanceLogFile(ServiceLocator habitat, Server targetServer, Domain domain, Logger logger, String instanceName, String domainRoot, String logFileName, String instanceLogFileName) throws IOException {
    File instanceLogFile = null;
    // method is used from logviewer back end code logfilter.
    // for Instance it's going through this loop. This will use ssh utility to get file from instance machine(remote machine) and
    // store in domains/domain1/logs/<instance name> which is used to get LogFile object.
    // Right now user needs to go through this URL to setup and configure ssh http://wikis.sun.com/display/GlassFish/3.1SSHSetup
    SSHLauncher sshL = getSSHL(habitat);
    String sNode = targetServer.getNodeRef();
    Nodes nodes = domain.getNodes();
    Node node = nodes.getNode(sNode);
    if (node.getType().equals("SSH")) {
        sshL.init(node, logger);
        SFTPClient sftpClient = sshL.getSFTPClient();
        File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs" + File.separator + instanceName);
        boolean noFileFound = true;
        String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);
        try {
            List instanceLogFileNames = sftpClient.ls(loggingDir);
            for (int i = 0; i < instanceLogFileNames.size(); i++) {
                SFTPv3DirectoryEntry file = (SFTPv3DirectoryEntry) instanceLogFileNames.get(i);
                String fileName = file.filename;
                // code to remove . and .. file which is return from sftpclient ls method
                if (!file.attributes.isDirectory() && !fileName.equals(".") && !fileName.equals("..") && fileName.contains(".log") && !fileName.contains(".log.")) {
                    noFileFound = false;
                    break;
                }
            }
        } catch (Exception e) {
            // if directory doesn't present or missing on remote machine. It happens due to bug 16451
            noFileFound = true;
        }
        if (noFileFound) {
            // this loop is used when user has changed value for server.log but not restarted the server.
            loggingDir = getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileName, node, sNode, instanceName);
        }
        String loggingFile = loggingDir + File.separator + logFileName;
        if (!sftpClient.exists(loggingFile)) {
            loggingFile = loggingDir + File.separator + "server.log";
        } else if (!sftpClient.exists(loggingFile)) {
            loggingFile = instanceLogFileName;
        }
        // creating local file name on DAS
        long instanceLogFileSize = 0;
        instanceLogFile = new File(logFileDirectoryOnServer.getAbsolutePath() + File.separator + loggingFile.substring(loggingFile.lastIndexOf(File.separator), loggingFile.length()));
        // getting size of the file on DAS
        if (instanceLogFile.exists()) {
            instanceLogFileSize = instanceLogFile.length();
        }
        SFTPv3FileAttributes sftPv3FileAttributes = sftpClient._stat(loggingFile);
        // getting size of the file on instance machine
        long fileSizeOnNode = sftPv3FileAttributes.size;
        // if differ both size then downloading
        if (instanceLogFileSize != fileSizeOnNode) {
            try (BufferedInputStream in = new BufferedInputStream(sftpClient.read(loggingFile));
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(instanceLogFile))) {
                int i;
                while ((i = in.read()) != -1) {
                    out.write(i);
                }
                out.flush();
            }
        }
        sftpClient.close();
    } else if (node.getType().equals("DCOM")) {
        File logFileDirectoryOnServer = makingDirectory(domainRoot + File.separator + "logs" + File.separator + instanceName);
        String loggingDir = getLoggingDirectoryForNode(instanceLogFileName, node, sNode, instanceName);
        try {
            DcomInfo info = new DcomInfo(node);
            WindowsRemoteFileSystem wrfs = new WindowsRemoteFileSystem(info.getHost(), info.getUser(), info.getPassword());
            if (logFileName == null || logFileName.equals("")) {
                logFileName = "server.log";
            }
            WindowsRemoteFile wrf = new WindowsRemoteFile(wrfs, loggingDir + File.separator + logFileName);
            instanceLogFile = new File(logFileDirectoryOnServer + File.separator + logFileName);
            wrf.copyTo(instanceLogFile);
        } catch (WindowsException ex) {
            throw new IOException("Unable to download instance log file from DCOM Instance Node");
        }
    }
    return instanceLogFile;
}
Also used : SSHLauncher(org.glassfish.cluster.ssh.launcher.SSHLauncher) Node(com.sun.enterprise.config.serverbeans.Node) SFTPClient(org.glassfish.cluster.ssh.sftp.SFTPClient) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) Nodes(com.sun.enterprise.config.serverbeans.Nodes) IOException(java.io.IOException) WindowsException(com.sun.enterprise.util.cluster.windows.process.WindowsException) DcomInfo(org.glassfish.cluster.ssh.util.DcomInfo) SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) WindowsRemoteFileSystem(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem) ArrayList(java.util.ArrayList) List(java.util.List) SFTPv3FileAttributes(com.trilead.ssh2.SFTPv3FileAttributes) WindowsRemoteFile(com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 4 with SFTPv3FileAttributes

use of com.trilead.ssh2.SFTPv3FileAttributes in project intellij-community by JetBrains.

the class SFTPv3Client method createAttrs.

private byte[] createAttrs(SFTPv3FileAttributes attr) {
    TypesWriter tw = new TypesWriter();
    int attrFlags = 0;
    if (attr == null) {
        tw.writeUINT32(0);
    } else {
        if (attr.size != null)
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE;
        if ((attr.uid != null) && (attr.gid != null))
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID;
        if (attr.permissions != null)
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS;
        if ((attr.atime != null) && (attr.mtime != null))
            attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME;
        tw.writeUINT32(attrFlags);
        if (attr.size != null)
            tw.writeUINT64(attr.size.longValue());
        if ((attr.uid != null) && (attr.gid != null)) {
            tw.writeUINT32(attr.uid.intValue());
            tw.writeUINT32(attr.gid.intValue());
        }
        if (attr.permissions != null)
            tw.writeUINT32(attr.permissions.intValue());
        if ((attr.atime != null) && (attr.mtime != null)) {
            tw.writeUINT32(attr.atime.intValue());
            tw.writeUINT32(attr.mtime.intValue());
        }
    }
    return tw.getBytes();
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 5 with SFTPv3FileAttributes

use of com.trilead.ssh2.SFTPv3FileAttributes in project intellij-community by JetBrains.

the class SFTPv3Client method statBoth.

private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_STAT/SSH_FXP_LSTAT...");
        debug.flush();
    }
    sendMessage(statMethod, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_ATTRS) {
        return readAttrs(tr);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Aggregations

TypesWriter (com.trilead.ssh2.packets.TypesWriter)6 SFTPv3FileAttributes (com.trilead.ssh2.SFTPv3FileAttributes)5 TypesReader (com.trilead.ssh2.packets.TypesReader)3 IOException (java.io.IOException)3 Node (com.sun.enterprise.config.serverbeans.Node)1 Nodes (com.sun.enterprise.config.serverbeans.Nodes)1 WindowsRemoteFile (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFile)1 WindowsRemoteFileSystem (com.sun.enterprise.util.cluster.windows.io.WindowsRemoteFileSystem)1 WindowsException (com.sun.enterprise.util.cluster.windows.process.WindowsException)1 SFTPv3DirectoryEntry (com.trilead.ssh2.SFTPv3DirectoryEntry)1 IOException2 (hudson.util.IOException2)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SSHLauncher (org.glassfish.cluster.ssh.launcher.SSHLauncher)1 SFTPClient (org.glassfish.cluster.ssh.sftp.SFTPClient)1 DcomInfo (org.glassfish.cluster.ssh.util.DcomInfo)1