1 2 29 30 package com.jcraft.jsch; 31 32 import java.text.SimpleDateFormat ; 33 import java.util.Date ; 34 35 49 public class SftpATTRS { 50 51 static final int S_ISUID = 04000; static final int S_ISGID = 02000; static final int S_ISVTX = 01000; 55 static final int S_IRUSR = 00400; static final int S_IWUSR = 00200; static final int S_IXUSR = 00100; static final int S_IREAD = 00400; static final int S_IWRITE= 00200; static final int S_IEXEC = 00100; 62 static final int S_IRGRP = 00040; static final int S_IWGRP = 00020; static final int S_IXGRP = 00010; 66 static final int S_IROTH = 00004; static final int S_IWOTH = 00002; static final int S_IXOTH = 00001; 70 private static final int pmask = 0xFFF; 71 72 public String getPermissionsString() { 73 StringBuffer buf = new StringBuffer (10); 74 75 if(isDir()) buf.append('d'); 76 else if(isLink()) buf.append('l'); 77 else buf.append('-'); 78 79 if((permissions & S_IRUSR)!=0) buf.append('r'); 80 else buf.append('-'); 81 82 if((permissions & S_IWUSR)!=0) buf.append('w'); 83 else buf.append('-'); 84 85 if((permissions & S_ISUID)!=0) buf.append('s'); 86 else if ((permissions & S_IXUSR)!=0) buf.append('x'); 87 else buf.append('-'); 88 89 if((permissions & S_IRGRP)!=0) buf.append('r'); 90 else buf.append('-'); 91 92 if((permissions & S_IWGRP)!=0) buf.append('w'); 93 else buf.append('-'); 94 95 if((permissions & S_ISGID)!=0) buf.append('s'); 96 else if((permissions & S_IXGRP)!=0) buf.append('x'); 97 else buf.append('-'); 98 99 if((permissions & S_IROTH) != 0) buf.append('r'); 100 else buf.append('-'); 101 102 if((permissions & S_IWOTH) != 0) buf.append('w'); 103 else buf.append('-'); 104 105 if((permissions & S_IXOTH) != 0) buf.append('x'); 106 else buf.append('-'); 107 return (buf.toString()); 108 } 109 110 public String getAtimeString(){ 111 SimpleDateFormat locale=new SimpleDateFormat (); 112 return (locale.format(new Date (atime))); 113 } 114 115 public String getMtimeString(){ 116 Date date= new Date (((long)mtime)*1000); 117 return (date.toString()); 118 } 119 120 public static final int SSH_FILEXFER_ATTR_SIZE= 0x00000001; 121 public static final int SSH_FILEXFER_ATTR_UIDGID= 0x00000002; 122 public static final int SSH_FILEXFER_ATTR_PERMISSIONS= 0x00000004; 123 public static final int SSH_FILEXFER_ATTR_ACMODTIME= 0x00000008; 124 public static final int SSH_FILEXFER_ATTR_EXTENDED= 0x80000000; 125 126 static final int S_IFDIR=0x4000; 127 static final int S_IFLNK=0xa000; 128 129 int flags=0; 130 long size; 131 int uid; 132 int gid; 133 int permissions; 134 int atime; 135 int mtime; 136 String [] extended=null; 137 138 private SftpATTRS(){ 139 } 140 141 static SftpATTRS getATTR(Buffer buf){ 142 SftpATTRS attr=new SftpATTRS(); 143 attr.flags=buf.getInt(); 144 if((attr.flags&SSH_FILEXFER_ATTR_SIZE)!=0){ attr.size=buf.getLong(); } 145 if((attr.flags&SSH_FILEXFER_ATTR_UIDGID)!=0){ 146 attr.uid=buf.getInt(); attr.gid=buf.getInt(); 147 } 148 if((attr.flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){ 149 attr.permissions=buf.getInt(); 150 } 151 if((attr.flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ 152 attr.atime=buf.getInt(); 153 } 154 if((attr.flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ 155 attr.mtime=buf.getInt(); 156 } 157 if((attr.flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){ 158 int count=buf.getInt(); 159 if(count>0){ 160 attr.extended=new String [count*2]; 161 for(int i=0; i<count; i++){ 162 attr.extended[i*2]=new String (buf.getString()); 163 attr.extended[i*2+1]=new String (buf.getString()); 164 } 165 } 166 } 167 return attr; 168 } 169 170 int length(){ 171 int len=4; 172 173 if((flags&SSH_FILEXFER_ATTR_SIZE)!=0){ len+=8; } 174 if((flags&SSH_FILEXFER_ATTR_UIDGID)!=0){ len+=8; } 175 if((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){ len+=4; } 176 if((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ len+=8; } 177 if((flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){ 178 len+=4; 179 int count=extended.length/2; 180 if(count>0){ 181 for(int i=0; i<count; i++){ 182 len+=4; len+=extended[i*2].length(); 183 len+=4; len+=extended[i*2+1].length(); 184 } 185 } 186 } 187 return len; 188 } 189 190 void dump(Buffer buf){ 191 buf.putInt(flags); 192 if((flags&SSH_FILEXFER_ATTR_SIZE)!=0){ buf.putLong(size); } 193 if((flags&SSH_FILEXFER_ATTR_UIDGID)!=0){ 194 buf.putInt(uid); buf.putInt(gid); 195 } 196 if((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){ 197 buf.putInt(permissions); 198 } 199 if((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ buf.putInt(atime); } 200 if((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ buf.putInt(mtime); } 201 if((flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){ 202 int count=extended.length/2; 203 if(count>0){ 204 for(int i=0; i<count; i++){ 205 buf.putString(extended[i*2].getBytes()); 206 buf.putString(extended[i*2+1].getBytes()); 207 } 208 } 209 } 210 } 211 void setFLAGS(int flags){ 212 this.flags=flags; 213 } 214 public void setSIZE(long size){ 215 flags|=SSH_FILEXFER_ATTR_SIZE; 216 this.size=size; 217 } 218 public void setUIDGID(int uid, int gid){ 219 flags|=SSH_FILEXFER_ATTR_UIDGID; 220 this.uid=uid; 221 this.gid=gid; 222 } 223 public void setACMODTIME(int atime, int mtime){ 224 flags|=SSH_FILEXFER_ATTR_ACMODTIME; 225 this.atime=atime; 226 this.mtime=mtime; 227 } 228 public void setPERMISSIONS(int permissions){ 229 flags|=SSH_FILEXFER_ATTR_PERMISSIONS; 230 permissions=(this.permissions&~pmask)|(permissions&pmask); 231 this.permissions=permissions; 232 } 233 234 public boolean isDir(){ 235 return ((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0 && 236 ((permissions&S_IFDIR)==S_IFDIR)); 237 } 238 public boolean isLink(){ 239 return ((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0 && 240 ((permissions&S_IFLNK)==S_IFLNK)); 241 } 242 public int getFlags() { return flags; } 243 public long getSize() { return size; } 244 public int getUId() { return uid; } 245 public int getGId() { return gid; } 246 public int getPermissions() { return permissions; } 247 public int getATime() { return atime; } 248 public int getMTime() { return mtime; } 249 public String [] getExtended() { return extended; } 250 251 public String toString() { 252 return (getPermissionsString()+" "+getUId()+" "+getGId()+" "+getSize()+" "+getMtimeString()); 253 } 254 263 } 264 | Popular Tags |