use of com.trilead.ssh2.SFTPException in project intellij-community by JetBrains.
the class SFTPv3Client method scanDirectory.
private final Vector scanDirectory(byte[] handle) throws IOException {
Vector files = new Vector();
while (true) {
int req_id = generateNextRequestID();
TypesWriter tw = new TypesWriter();
tw.writeString(handle, 0, handle.length);
if (debug != null) {
debug.println("Sending SSH_FXP_READDIR...");
debug.flush();
}
sendMessage(Packet.SSH_FXP_READDIR, req_id, tw.getBytes());
/* Some servers send here a packet with size > 34000 */
/* To whom it may concern: please learn to read the specs. */
byte[] resp = receiveMessage(65536);
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_NAME) {
int count = tr.readUINT32();
if (debug != null)
debug.println("Parsing " + count + " name entries...");
while (count > 0) {
SFTPv3DirectoryEntry dirEnt = new SFTPv3DirectoryEntry();
dirEnt.filename = tr.readString(charsetName);
dirEnt.longEntry = tr.readString(charsetName);
dirEnt.attributes = readAttrs(tr);
files.addElement(dirEnt);
if (debug != null)
debug.println("File: '" + dirEnt.filename + "'");
count--;
}
continue;
}
if (t != Packet.SSH_FXP_STATUS)
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
int errorCode = tr.readUINT32();
if (errorCode == ErrorCodes.SSH_FX_EOF)
return files;
throw new SFTPException(tr.readString(), errorCode);
}
}
use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.
the class SFTPv3Client method fstat.
/**
* Retrieve the file attributes of an open file.
*
* @param handle a SFTPv3FileHandle handle.
* @return a SFTPv3FileAttributes object.
* @throws IOException
*/
public SFTPv3FileAttributes fstat(SFTPv3FileHandle handle) throws IOException {
checkHandleValidAndOpen(handle);
int req_id = generateNextRequestID();
TypesWriter tw = new TypesWriter();
tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
if (debug != null) {
debug.println("Sending SSH_FXP_FSTAT...");
debug.flush();
}
sendMessage(Packet.SSH_FXP_FSTAT, 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);
}
use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.
the class SFTPv3Client method openDirectory.
private final byte[] openDirectory(String path) throws IOException {
int req_id = generateNextRequestID();
TypesWriter tw = new TypesWriter();
tw.writeString(path, charsetName);
if (debug != null) {
debug.println("Sending SSH_FXP_OPENDIR...");
debug.flush();
}
sendMessage(Packet.SSH_FXP_OPENDIR, req_id, tw.getBytes());
byte[] resp = receiveMessage(34000);
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_HANDLE) {
if (debug != null) {
debug.println("Got SSH_FXP_HANDLE.");
debug.flush();
}
byte[] handle = tr.readByteString();
return handle;
}
if (t != Packet.SSH_FXP_STATUS)
throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
int errorCode = tr.readUINT32();
String errorMessage = tr.readString();
throw new SFTPException(errorMessage, errorCode);
}
use of com.trilead.ssh2.SFTPException in project agileway by fangjinuo.
the class Ssh2SftpSession method open.
@Override
public SftpFile open(String filepath, int openMode, FileAttrs attrs) throws SftpException {
try {
SFTPv3FileHandle handle = null;
if (!Sftps.exists(this, filepath)) {
if (attrs == null) {
attrs = new FileAttrs();
}
FileMode fileMode = attrs.getFileMode();
if (fileMode == null) {
fileMode = FileMode.createFileMode(FileType.REGULAR, 0644);
attrs.setFileMode(fileMode);
}
if (OpenMode.isCreatable(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
} else {
throw new NoSuchFileSftpException(StringTemplates.formatWithPlaceholder("no such file: {}", filepath));
}
} else {
if (OpenMode.isTruncated(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
}
}
if (handle != null) {
sftpClient.closeFile(handle);
handle = null;
}
if (OpenMode.isWritable(openMode)) {
handle = sftpClient.openFileRW(filepath);
} else {
handle = sftpClient.openFileRO(filepath);
}
Ssh2SftpFile sftpFile = new Ssh2SftpFile(this, filepath);
sftpFile.setFileHandle(handle);
return sftpFile;
} catch (Throwable ex) {
throw new SftpException(ex);
}
}
use of com.trilead.ssh2.SFTPException in project agileway by fangjinuo.
the class Ssh2SftpSession method setStat.
@Override
public void setStat(String path, FileAttrs attrs) throws SftpException {
try {
SFTPv3FileAttributes attributes = Ssh2Sftps.toSsh2FileAttributes(attrs);
sftpClient.setstat(path, attributes);
} catch (Throwable ex) {
throw Ssh2Sftps.wrapSftpException(ex);
}
}
Aggregations