}
sendOPENDIR(Util.str2byte(dir, _fileEncoding));
readResponse();
if( _header.type != SSH_FXP_HANDLE ) {
throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
}
byte[] handle = _buffer.getString(); // handle
List<LsEntry> lsEntries = new ArrayList<LsEntry>();
while( true ) {
sendREADDIR(handle);
readHeader();
if( _header.type != SSH_FXP_STATUS && _header.type != SSH_FXP_NAME ) {
throw new SftpException(SSH_FX_FAILURE, "Invalid FXP response: "+_header.type);
} else if( _header.type == SSH_FXP_STATUS ) {
fill(_buffer, _header.length);
int i = _buffer.getInt();
if( i == SSH_FX_EOF ) {
break;
}
throwStatusError(_buffer, i);
}
_buffer.rewind();
fill(_buffer.buffer, 0, 4);
_header.length -= 4;
int count = _buffer.getInt();
_buffer.reset();
while( count > 0 ) {
if( _header.length > 0 ) {
_buffer.shift();
int j = (_buffer.buffer.length > (_buffer.index + _header.length)) ? _header.length : (_buffer.buffer.length - _buffer.index);
int i = fill(_buffer.buffer, _buffer.index, j);
_buffer.index += i;
_header.length -= i;
}
byte[] bFilename = _buffer.getString();
String sFilename = Util.byte2str(bFilename, _fileEncoding);
byte[] bLongname = _serverVersion <= 3 ? _buffer.getString() : null;
SftpATTRS attrs = SftpATTRS.getATTR(_buffer);
boolean found = false;
if( bPattern == null ) {
found = true;
} else if( !wildcardPattern ) {
found = Arrays.equals(bPattern, bFilename);
} else {
found = Util.glob(bPattern, _utf8 ? bFilename : Util.str2byte(sFilename, UTF8));
}
if( found ) {
// TODO: need to generate long name from attrs for sftp protocol 4(and later)
String sLongname = bLongname == null ?
(attrs.toString() + " " + sFilename) :
Util.byte2str(bLongname, _fileEncoding);
lsEntries.add(new LsEntry(sFilename, sLongname, attrs));
}
count--;
}
}
_sendCLOSE(handle);
return lsEntries;
} catch(SftpException e) {
throw e;
} catch(Exception e) {
throw new SftpException(SSH_FX_FAILURE, "Failed to ls path: "+path, e);
}
}