public void mkdir(String path) throws SftpException{
try{
path=remoteAbsolutePath(path); // 기존 경로에 붙여서 제공
sendMKDIR(Util.str2byte(path, fEncoding), null); // 기존 경로의 utf-8
Header header=new Header(); // SSH_FXP_STATUS = 101
header=header(buf, header);
int length=header.length;
int type=header.type;
fill(buf, length);
if(type!=SSH_FXP_STATUS){
throw new SftpException(SSH_FX_FAILURE, "");
}
int i=buf.getInt();
if(i==SSH_FX_OK) return;
throwStatusError(buf, i);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
private String remoteAbsolutePath(String path) throws SftpException{
if(path.charAt(0)=='/') return path;
String cwd=getCwd();
// if(cwd.equals(getHome())) return path;
if(cwd.endsWith("/")) return cwd+path;
return cwd+"/"+path; // 해당 경로 뒤에다가 패스 경로 만들어줌!
}
private String getCwd() throws SftpException{
if(cwd==null) // cwd 값은 .cd() 메서드 실행 시 들어가게 됨! 이미 한번 실행한 과거 이력이 있으면 cwd 값 존재!
cwd=getHome();
return cwd;
}
public String getHome() throws SftpException {
if(home==null){
try{
byte[] _home=_realpath("");
home=Util.byte2str(_home, fEncoding); // fEncoding = UTF8
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
return home;
}
static String byte2str(byte[] str, String encoding){
return byte2str(str, 0, str.length, encoding);
}
static String byte2str(byte[] str, int s, int l, String encoding){
try{ return new String(str, s, l, encoding); }
catch(java.io.UnsupportedEncodingException e){
return new String(str, s, l);
}
}
private void sendMKDIR(byte[] path, SftpATTRS attr) throws Exception{
packet.reset();
putHEAD(SSH_FXP_MKDIR, 9+path.length+(attr!=null?attr.length():4));
buf.putInt(seq++);
buf.putString(path); // path
if(attr!=null) attr.dump(buf);
else buf.putInt(0);
getSession().write(packet, this, 9+path.length+(attr!=null?attr.length():4)+4);
}
private void putHEAD(byte type, int length) throws Exception{
buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
buf.putInt(recipient);
buf.putInt(length+4);
buf.putInt(length);
buf.putByte(type);
}
Header
private Header header(Buffer buf, Header header) throws IOException{
buf.rewind();
int i=fill(buf.buffer, 0, 9);
header.length=buf.getInt()-5;
header.type=buf.getByte()&0xff; // 0xff -> 16진수
header.rid=buf.getInt();
return header;
}