@Override protected void doCloseCommunicationLink() { if ( this.session != null ) { this.session.disconnect(); this.session = null; } super.doCloseCommunicationLink(); }
private void flushStat() throws IOException, SftpException { final ChannelSftp channel = getAbstractFileSystem().getChannel(); try { channel.setStat(relPath, attrs); } finally { getAbstractFileSystem().putChannel(channel); } }
doCloseCommunicationLink(); try final GenericFileName rootName = (GenericFileName) getRootName(); authData = UserAuthenticatorUtils.authenticate(getFileSystemOptions(), SftpFileProvider.AUTHENTICATOR_TYPES); UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null), getFileSystemOptions()); throw new FileSystemException("vfs.provider.sftp/connect.error", getRootName(), e);
ensureSession(); try { channel.connect(); final Boolean userDirIsRoot = SftpFileSystemConfigBuilder.getInstance() .getUserDirIsRoot(getFileSystemOptions()); final String workingDirectory = getRootName().getPath(); if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue())) { try { .getFileNameEncoding(getFileSystemOptions()); throw new FileSystemException("vfs.provider.sftp/connect.error", getRootName(), e);
/** * Returns the POSIX type permissions of the file. * * @param checkIds {@code true} if user and group ID should be checked (needed for some access rights checks) * @return A PosixPermission object * @throws Exception If an error occurs * @since 2.1 */ protected PosixPermissions getPermissions(final boolean checkIds) throws Exception { statSelf(); boolean isInGroup = false; if (checkIds) { for (final int groupId : getAbstractFileSystem().getGroupsIds()) { if (groupId == attrs.getGId()) { isInGroup = true; break; } } } final boolean isOwner = checkIds ? attrs.getUId() == getAbstractFileSystem().getUId() : false; return new PosixPermissions(attrs.getPermissions(), isOwner, isInGroup); }
/** * Called after the stream has been closed. */ @Override protected void onClose() throws IOException { getAbstractFileSystem().putChannel(channel); } }
/** * Creates an output stream to write the file content to. */ @Override protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception { // TODO - Don't write the entire file into memory. Use the stream-based // methods on ChannelSftp once the work properly /* * final ChannelSftp channel = getAbstractFileSystem().getChannel(); return new SftpOutputStream(channel); */ final ChannelSftp channel = getAbstractFileSystem().getChannel(); return new SftpOutputStream(channel, channel.put(relPath)); }
/** * Get the (numeric) group IDs. * * @return The numeric user ID * @throws JSchException If a problem occurs while retrieving the group ID. * @throws IOException if an I/O error is detected. * @since 2.1 */ public int getUId() throws JSchException, IOException { if (uid < 0) { final StringBuilder output = new StringBuilder(); final int code = executeCommand("id -u", output); if (code != 0) { throw new FileSystemException( "Could not get the user id of the current user (error code: " + code + ")"); } uid = Integer.parseInt(output.toString().trim()); } return uid; }
protected SftpFileObject(final AbstractFileName name, final SftpFileSystem fileSystem) throws FileSystemException { super(name, fileSystem); relPath = UriParser.decode(fileSystem.getRootName().getRelativeName( name)); }
return new SftpFileSystem(rootName, session, fileSystemOptions);
ensureSession(); final ChannelExec channel = (ChannelExec) session.openChannel("exec");
/** * Creates this file as a folder. */ @Override protected void doCreateFolder() throws Exception { final ChannelSftp channel = getAbstractFileSystem().getChannel(); try { channel.mkdir(relPath); } finally { getAbstractFileSystem().putChannel(channel); } }
/** * Ensures that the session link is established. * * @throws FileSystemException if a session cannot be created. */ private void ensureSession() throws FileSystemException { if (this.session == null || !this.session.isConnected()) { doCloseCommunicationLink(); // channel closed. e.g. by freeUnusedResources, but now we need it again Session session; UserAuthenticationData authData = null; try { final GenericFileName rootName = (GenericFileName) getRootName(); authData = UserAuthenticatorUtils.authenticate(getFileSystemOptions(), SftpFileProvider.AUTHENTICATOR_TYPES); session = SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(), UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())), UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())), getFileSystemOptions()); } catch (final Exception e) { throw new FileSystemException("vfs.provider.sftp/connect.error", getRootName(), e); } finally { UserAuthenticatorUtils.cleanup(authData); } this.session = session; } }
ensureSession(); try { channel.connect(); final Boolean userDirIsRoot = SftpFileSystemConfigBuilder.getInstance() .getUserDirIsRoot(getFileSystemOptions()); final String workingDirectory = getRootName().getPath(); if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue())) { try { .getFileNameEncoding(getFileSystemOptions()); throw new FileSystemException("vfs.provider.sftp/connect.error", getRootName(), e);
/** * Returns the POSIX type permissions of the file. * * @param checkIds {@code true} if user and group ID should be checked (needed for some access rights checks) * @return A PosixPermission object * @throws Exception If an error occurs * @since 2.1 */ protected PosixPermissions getPermissions(final boolean checkIds) throws Exception { statSelf(); boolean isInGroup = false; if (checkIds) { for (final int groupId : getAbstractFileSystem().getGroupsIds()) { if (groupId == attrs.getGId()) { isInGroup = true; break; } } } final boolean isOwner = checkIds ? attrs.getUId() == getAbstractFileSystem().getUId() : false; return new PosixPermissions(attrs.getPermissions(), isOwner, isInGroup); }
/** * Called after this stream is closed. */ @Override protected void onClose() throws IOException { getAbstractFileSystem().putChannel(channel); } }
/** * Creates an output stream to write the file content to. */ @Override protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception { // TODO - Don't write the entire file into memory. Use the stream-based // methods on ChannelSftp once the work properly /* * final ChannelSftp channel = getAbstractFileSystem().getChannel(); return new SftpOutputStream(channel); */ final ChannelSftp channel = getAbstractFileSystem().getChannel(); return new SftpOutputStream(channel, channel.put(relPath)); }
/** * Get the (numeric) group IDs. * * @return The numeric user ID * @throws JSchException If a problem occurs while retrieving the group ID. * @throws IOException if an I/O error is detected. * @since 2.1 */ public int getUId() throws JSchException, IOException { if (uid < 0) { final StringBuilder output = new StringBuilder(); final int code = executeCommand("id -u", output); if (code != 0) { throw new FileSystemException( "Could not get the user id of the current user (error code: " + code + ")"); } uid = Integer.parseInt(output.toString().trim()); } return uid; }
protected SftpFileObject(final AbstractFileName name, final SftpFileSystem fileSystem) throws FileSystemException { super(name, fileSystem); relPath = UriParser.decode(fileSystem.getRootName().getRelativeName(name)); }
/** * Creates a {@link FileSystem}. */ @Override protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) throws FileSystemException { // JSch jsch = createJSch(fileSystemOptions); // Create the file system final GenericFileName rootName = (GenericFileName) name; Session session; UserAuthenticationData authData = null; try { authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES); session = SftpClientFactory.createConnection(rootName.getHostName(), rootName.getPort(), UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName())), UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword())), fileSystemOptions); } catch (final Exception e) { throw new FileSystemException("vfs.provider.sftp/connect.error", name, e); } finally { UserAuthenticatorUtils.cleanup(authData); } return new SftpFileSystem(rootName, session, fileSystemOptions); }