package org.apache.commons.vfs2.provider.sftp;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
import org.apache.commons.vfs2.FileNotFoundException;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.NameScope;
import org.apache.commons.vfs2.RandomAccessContent;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.provider.AbstractFileName;
import org.apache.commons.vfs2.provider.AbstractFileObject;
import org.apache.commons.vfs2.provider.UriParser;
import org.apache.commons.vfs2.util.FileObjectUtils;
import org.apache.commons.vfs2.util.MonitorInputStream;
import org.apache.commons.vfs2.util.MonitorOutputStream;
import org.apache.commons.vfs2.util.PosixPermissions;
import org.apache.commons.vfs2.util.RandomAccessMode;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
public class SftpFileObject extends AbstractFileObject<SftpFileSystem> {
private static final long MOD_TIME_FACTOR = 1000L;
private SftpATTRS attrs;
private final String relPath;
private boolean inRefresh;
protected SftpFileObject(final AbstractFileName name, final SftpFileSystem fileSystem) throws FileSystemException {
super(name, fileSystem);
relPath = UriParser.decode(fileSystem.getRootName().getRelativeName(name));
}
@Override
protected void doDetach() throws Exception {
attrs = null;
}
@Override
protected FileType doGetType() throws Exception {
if (attrs == null) {
statSelf();
}
if (attrs == null) {
return FileType.IMAGINARY;
}
if ((attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS) == 0) {
throw new FileSystemException("vfs.provider.sftp/unknown-permissions.error");
}
if (attrs.isDir()) {
return FileType.FOLDER;
}
return FileType.FILE;
}
@Override
protected void onChange() throws Exception {
statSelf();
}
private void statSelf() throws IOException {
ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
setStat(channel.stat(relPath));
} catch (final SftpException e) {
try {
if (e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE) {
channel.disconnect();
channel = getAbstractFileSystem().getChannel();
setStat(channel.stat(relPath));
} else {
attrs = null;
}
} catch (final SftpException innerEx) {
attrs = null;
}
} finally {
getAbstractFileSystem().putChannel(channel);
}
}
private void setStat(final SftpATTRS attrs) {
this.attrs = attrs;
}
@Override
protected void doCreateFolder() throws Exception {
final ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
channel.mkdir(relPath);
} finally {
getAbstractFileSystem().putChannel(channel);
}
}
@Override
protected long doGetLastModifiedTime() throws Exception {
if (attrs == null || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) == 0) {
throw new FileSystemException("vfs.provider.sftp/unknown-modtime.error");
}
return attrs.getMTime() * MOD_TIME_FACTOR;
}
@Override
protected boolean doSetLastModifiedTime(final long modtime) throws Exception {
final int newMTime = (int) (modtime / MOD_TIME_FACTOR);
attrs.setACMODTIME(attrs.getATime(), newMTime);
flushStat();
return true;
}
private void flushStat() throws IOException, SftpException {
final ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
channel.setStat(relPath, attrs);
} finally {
getAbstractFileSystem().putChannel(channel);
}
}
@Override
protected void doDelete() throws Exception {
final ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
if (isFile()) {
channel.rm(relPath);
} else {
channel.rmdir(relPath);
}
} finally {
getAbstractFileSystem().putChannel(channel);
}
}
@Override
protected void doRename(final FileObject newFile) throws Exception {
final ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
final SftpFileObject newSftpFileObject = (SftpFileObject) FileObjectUtils.getAbstractFileObject(newFile);
channel.rename(relPath, newSftpFileObject.relPath);
} finally {
getAbstractFileSystem().putChannel(channel);
}
}
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);
}
@Override
protected boolean doIsReadable() throws Exception {
return getPermissions(true).isReadable();
}
@Override
protected boolean doSetReadable(final boolean readable, final boolean ownerOnly) throws Exception {
final PosixPermissions permissions = getPermissions(false);
final int newPermissions = permissions.makeReadable(readable, ownerOnly);
if (newPermissions == permissions.getPermissions()) {
return true;
}
attrs.setPERMISSIONS(newPermissions);
flushStat();
return true;
}
@Override
protected boolean doIsWriteable() throws Exception {
return getPermissions(true).isWritable();
}
@Override
protected boolean doSetWritable(final boolean writable, final boolean ownerOnly) throws Exception {
final PosixPermissions permissions = getPermissions(false);
final int newPermissions = permissions.makeWritable(writable, ownerOnly);
if (newPermissions == permissions.getPermissions()) {
return true;
}
attrs.setPERMISSIONS(newPermissions);
flushStat();
return true;
}
@Override
protected boolean doIsExecutable() throws Exception {
return getPermissions(true).isExecutable();
}
@Override
protected boolean doSetExecutable(final boolean executable, final boolean ownerOnly) throws Exception {
final PosixPermissions permissions = getPermissions(false);
final int newPermissions = permissions.makeExecutable(executable, ownerOnly);
if (newPermissions == permissions.getPermissions()) {
return true;
}
attrs.setPERMISSIONS(newPermissions);
flushStat();
return true;
}
@Override
protected FileObject[] doListChildrenResolved() throws Exception {
if (this.isFile()) {
return null;
}
Vector<?> vector = null;
final ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
vector = channel.ls(relPath);
} catch (final SftpException e) {
String workingDirectory = null;
try {
if (relPath != null) {
workingDirectory = channel.pwd();
channel.cd(relPath);
}
} catch (final SftpException ex) {
return null;
}
SftpException lsEx = null;
try {
vector = channel.ls(".");
} catch (final SftpException ex) {
lsEx = ex;
} finally {
try {
if (relPath != null) {
channel.cd(workingDirectory);
}
} catch (final SftpException xe) {
throw new FileSystemException("vfs.provider.sftp/change-work-directory-back.error",
workingDirectory, lsEx);
}
}
if (lsEx != null) {
throw lsEx;
}
} finally {
getAbstractFileSystem().putChannel(channel);
}
FileSystemException.requireNonNull(vector, "vfs.provider.sftp/list-children.error");
final ArrayList<FileObject> children = new ArrayList<>();
for (@SuppressWarnings("unchecked")
final Iterator<LsEntry> iterator = (Iterator<LsEntry>) vector.iterator(); iterator.hasNext();) {
final LsEntry stat = iterator.next();
String name = stat.getFilename();
if (VFS.isUriStyle() && stat.getAttrs().isDir() && name.charAt(name.length() - 1) != '/') {
name = name + "/";
}
if (name.equals(".") || name.equals("..") || name.equals("./") || name.equals("../")) {
continue;
}
final FileObject fo = getFileSystem().resolveFile(getFileSystem().getFileSystemManager()
.resolveName(getName(), UriParser.encode(name), NameScope.CHILD));
((SftpFileObject) FileObjectUtils.getAbstractFileObject(fo)).setStat(stat.getAttrs());
children.add(fo);
}
return children.toArray(new FileObject[children.size()]);
}
@Override
protected String[] doListChildren() throws Exception {
return null;
}
@Override
protected long doGetContentSize() throws Exception {
if (attrs == null || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) == 0) {
throw new FileSystemException("vfs.provider.sftp/unknown-size.error");
}
return attrs.getSize();
}
@Override
protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception {
return new SftpRandomAccessContent(this, mode);
}
InputStream getInputStream(final long filePointer) throws IOException {
final ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
return new SftpInputStream(channel, channel.get(getName().getPathDecoded(), null, filePointer));
} catch (final SftpException e) {
getAbstractFileSystem().putChannel(channel);
throw new FileSystemException(e);
}
}
@Override
protected InputStream doGetInputStream() throws Exception {
synchronized (getAbstractFileSystem()) {
final ChannelSftp channel = getAbstractFileSystem().getChannel();
try {
InputStream is;
try {
if (!getType().hasContent()) {
throw new FileSystemException("vfs.provider/read-not-file.error", getName());
}
is = channel.get(relPath);
} catch (final SftpException e) {
if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
throw new FileNotFoundException(getName());
}
throw new FileSystemException(e);
}
return new SftpInputStream(channel, is);
} finally {
}
}
}
@Override
protected OutputStream doGetOutputStream(final boolean bAppend) throws Exception {
final ChannelSftp channel = getAbstractFileSystem().getChannel();
return new SftpOutputStream(channel, channel.put(relPath, bAppend ? ChannelSftp.APPEND : ChannelSftp.OVERWRITE));
}
private class SftpInputStream extends MonitorInputStream {
private final ChannelSftp channel;
public SftpInputStream(final ChannelSftp channel, final InputStream in) {
super(in);
this.channel = channel;
}
@Override
protected void onClose() throws IOException {
getAbstractFileSystem().putChannel(channel);
}
}
private class SftpOutputStream extends MonitorOutputStream {
private final ChannelSftp channel;
public SftpOutputStream(final ChannelSftp channel, final OutputStream out) {
super(out);
this.channel = channel;
}
@Override
protected void onClose() throws IOException {
getAbstractFileSystem().putChannel(channel);
}
}
}