SftpATTRS.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 import java.text.SimpleDateFormat;
00033 import java.util.Date;
00034 
00079 public class SftpATTRS {
00080 
00081   static final int S_ISUID = 04000; // set user ID on execution
00082   static final int S_ISGID = 02000; // set group ID on execution
00083   static final int S_ISVTX = 01000; // sticky bit   ****** NOT DOCUMENTED *****
00084 
00085   static final int S_IRUSR = 00400; // read by owner
00086   static final int S_IWUSR = 00200; // write by owner
00087   static final int S_IXUSR = 00100; // execute/search by owner
00088   static final int S_IREAD = 00400; // read by owner
00089   static final int S_IWRITE= 00200; // write by owner
00090   static final int S_IEXEC = 00100; // execute/search by owner
00091 
00092   static final int S_IRGRP = 00040; // read by group
00093   static final int S_IWGRP = 00020; // write by group
00094   static final int S_IXGRP = 00010; // execute/search by group
00095 
00096   static final int S_IROTH = 00004; // read by others
00097   static final int S_IWOTH = 00002; // write by others
00098   static final int S_IXOTH = 00001; // execute/search by others
00099 
00104   private static final int pmask = 0xFFF;
00105 
00106 
00111   public String getPermissionsString() {
00112     StringBuffer buf = new StringBuffer(10);
00113 
00114     if(isDir()) buf.append('d');
00115     else if(isLink()) buf.append('l');
00116     else buf.append('-');
00117 
00118     if((permissions & S_IRUSR)!=0) buf.append('r');
00119     else buf.append('-');
00120 
00121     if((permissions & S_IWUSR)!=0) buf.append('w');
00122     else buf.append('-');
00123 
00124     if((permissions & S_ISUID)!=0) buf.append('s');
00125     else if ((permissions & S_IXUSR)!=0) buf.append('x');
00126     else buf.append('-');
00127 
00128     if((permissions & S_IRGRP)!=0) buf.append('r');
00129     else buf.append('-');
00130 
00131     if((permissions & S_IWGRP)!=0) buf.append('w');
00132     else buf.append('-');
00133 
00134     if((permissions & S_ISGID)!=0) buf.append('s');
00135     else if((permissions & S_IXGRP)!=0) buf.append('x');
00136     else buf.append('-');
00137 
00138     if((permissions & S_IROTH) != 0) buf.append('r');
00139     else buf.append('-');
00140 
00141     if((permissions & S_IWOTH) != 0) buf.append('w');
00142     else buf.append('-');
00143 
00144     if((permissions & S_IXOTH) != 0) buf.append('x');
00145     else buf.append('-');
00146     return (buf.toString());
00147   }
00148 
00152   public String  getAtimeString(){
00153     SimpleDateFormat locale=new SimpleDateFormat();
00154     // shouldn't this be  new Date(1000L*atime)   ? -- P.E.
00155     return (locale.format(new Date(atime)));
00156   }
00157 
00161   public String  getMtimeString(){
00162     Date date= new Date(((long)mtime)*1000);
00163     return (date.toString());
00164   }
00165 
00170   public static final int SSH_FILEXFER_ATTR_SIZE=         0x00000001;
00175   public static final int SSH_FILEXFER_ATTR_UIDGID=       0x00000002;
00180   public static final int SSH_FILEXFER_ATTR_PERMISSIONS=  0x00000004;
00185   public static final int SSH_FILEXFER_ATTR_ACMODTIME=    0x00000008;
00190   public static final int SSH_FILEXFER_ATTR_EXTENDED=     0x80000000;
00191 
00192   static final int S_IFDIR=0x4000;
00193   static final int S_IFLNK=0xa000;
00194 
00195   int flags=0;
00196   long size;
00197   int uid;
00198   int gid;
00199   int permissions;
00200   int atime;
00201   int mtime;
00202   String[] extended=null;
00203 
00204   private SftpATTRS(){
00205   }
00206 
00210   static SftpATTRS getATTR(Buffer buf){
00211     SftpATTRS attr=new SftpATTRS(); 
00212     attr.flags=buf.getInt();
00213     if((attr.flags&SSH_FILEXFER_ATTR_SIZE)!=0){ attr.size=buf.getLong(); }
00214     if((attr.flags&SSH_FILEXFER_ATTR_UIDGID)!=0){
00215       attr.uid=buf.getInt(); attr.gid=buf.getInt();
00216     }
00217     if((attr.flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){ 
00218       attr.permissions=buf.getInt();
00219     }
00220     if((attr.flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ 
00221       attr.atime=buf.getInt();
00222     }
00223     if((attr.flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ 
00224       attr.mtime=buf.getInt(); 
00225     }
00226     if((attr.flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){
00227       int count=buf.getInt();
00228       if(count>0){
00229     attr.extended=new String[count*2];
00230     for(int i=0; i<count; i++){
00231       attr.extended[i*2]=Util.byte2str(buf.getString());
00232       attr.extended[i*2+1]=Util.byte2str(buf.getString());
00233     }
00234       }
00235     }
00236     return attr;
00237   } 
00238 
00243   int length(){
00244     int len=4;
00245 
00246     if((flags&SSH_FILEXFER_ATTR_SIZE)!=0){ len+=8; }
00247     if((flags&SSH_FILEXFER_ATTR_UIDGID)!=0){ len+=8; }
00248     if((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){ len+=4; }
00249     if((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ len+=8; }
00250     if((flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){
00251       len+=4;
00252       int count=extended.length/2;
00253       if(count>0){
00254     for(int i=0; i<count; i++){
00255       len+=4; len+=extended[i*2].length();
00256       len+=4; len+=extended[i*2+1].length();
00257     }
00258       }
00259     }
00260     return len;
00261   }
00262 
00266   void dump(Buffer buf){
00267     buf.putInt(flags);
00268     if((flags&SSH_FILEXFER_ATTR_SIZE)!=0){ buf.putLong(size); }
00269     if((flags&SSH_FILEXFER_ATTR_UIDGID)!=0){
00270       buf.putInt(uid); buf.putInt(gid);
00271     }
00272     if((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){ 
00273       buf.putInt(permissions);
00274     }
00275     if((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ buf.putInt(atime); }
00276     if((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){ buf.putInt(mtime); }
00277     if((flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){
00278       int count=extended.length/2;
00279       // where is the buf.putInt(count)?  -- P.E.
00280       if(count>0){
00281     for(int i=0; i<count; i++){
00282       buf.putString(Util.str2byte(extended[i*2]));
00283       buf.putString(Util.str2byte(extended[i*2+1]));
00284     }
00285       }
00286     }
00287   }
00288 
00292   void setFLAGS(int flags){
00293     this.flags=flags;
00294   }
00295 
00299   public void setSIZE(long size){
00300     flags|=SSH_FILEXFER_ATTR_SIZE;
00301     this.size=size;
00302   }
00303 
00307   public void setUIDGID(int uid, int gid){
00308     flags|=SSH_FILEXFER_ATTR_UIDGID;
00309     this.uid=uid;
00310     this.gid=gid;
00311   }
00312 
00316   public void setACMODTIME(int atime, int mtime){
00317     flags|=SSH_FILEXFER_ATTR_ACMODTIME;
00318     this.atime=atime;
00319     this.mtime=mtime;
00320   }
00321 
00327   public void setPERMISSIONS(int permissions){
00328     flags|=SSH_FILEXFER_ATTR_PERMISSIONS;
00329     permissions=(this.permissions&~pmask)|(permissions&pmask);
00330     this.permissions=permissions;
00331   }
00332 
00338   public boolean isDir(){
00339     return ((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0 && 
00340         ((permissions&S_IFDIR)==S_IFDIR));
00341   }
00342 
00348   public boolean isLink(){
00349     return ((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0 && 
00350         ((permissions&S_IFLNK)==S_IFLNK));
00351   }
00352 
00357   public int getFlags() { return flags; }
00358 
00362   public long getSize() { return size; }
00363 
00371   public int getUId() { return uid; }
00372 
00380   public int getGId() { return gid; }
00381 
00389   public int getPermissions() { return permissions; }
00390 
00399   public int getATime() { return atime; }
00400 
00409   public int getMTime() { return mtime; }
00415   public String[] getExtended() { return extended; }
00416 
00421   public String toString() {
00422     return (getPermissionsString()+" "+getUId()+" "+getGId()+" "+getSize()+" "+getMtimeString());
00423   }
00424   /*
00425   public String toString(){
00426     return (((flags&SSH_FILEXFER_ATTR_SIZE)!=0) ? ("size:"+size+" ") : "")+
00427            (((flags&SSH_FILEXFER_ATTR_UIDGID)!=0) ? ("uid:"+uid+",gid:"+gid+" ") : "")+
00428            (((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0) ? ("permissions:0x"+Integer.toHexString(permissions)+" ") : "")+
00429            (((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0) ? ("atime:"+atime+",mtime:"+mtime+" ") : "")+
00430            (((flags&SSH_FILEXFER_ATTR_EXTENDED)!=0) ? ("extended:?"+" ") : "");
00431   }
00432   */
00433 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1