]> Joshua Wise's Git repositories - dumload.git/blob - src/com/jcraft/jsch/SftpATTRS.java
Initial commit.
[dumload.git] / src / com / jcraft / jsch / SftpATTRS.java
1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright 
12      notice, this list of conditions and the following disclaimer in 
13      the documentation and/or other materials provided with the distribution.
14
15   3. The names of the authors may not be used to endorse or promote products
16      derived from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 package com.jcraft.jsch;
31
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
34
35 /*
36   uint32   flags
37   uint64   size           present only if flag SSH_FILEXFER_ATTR_SIZE
38   uint32   uid            present only if flag SSH_FILEXFER_ATTR_UIDGID
39   uint32   gid            present only if flag SSH_FILEXFER_ATTR_UIDGID
40   uint32   permissions    present only if flag SSH_FILEXFER_ATTR_PERMISSIONS
41   uint32   atime          present only if flag SSH_FILEXFER_ACMODTIME
42   uint32   mtime          present only if flag SSH_FILEXFER_ACMODTIME
43   uint32   extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED
44   string   extended_type
45   string   extended_data
46     ...      more extended data (extended_type - extended_data pairs),
47              so that number of pairs equals extended_count
48 */
49 public class SftpATTRS {
50
51   static final int S_ISUID = 04000; // set user ID on execution
52   static final int S_ISGID = 02000; // set group ID on execution
53   static final int S_ISVTX = 01000; // sticky bit   ****** NOT DOCUMENTED *****
54
55   static final int S_IRUSR = 00400; // read by owner
56   static final int S_IWUSR = 00200; // write by owner
57   static final int S_IXUSR = 00100; // execute/search by owner
58   static final int S_IREAD = 00400; // read by owner
59   static final int S_IWRITE= 00200; // write by owner
60   static final int S_IEXEC = 00100; // execute/search by owner
61
62   static final int S_IRGRP = 00040; // read by group
63   static final int S_IWGRP = 00020; // write by group
64   static final int S_IXGRP = 00010; // execute/search by group
65
66   static final int S_IROTH = 00004; // read by others
67   static final int S_IWOTH = 00002; // write by others
68   static final int S_IXOTH = 00001; // execute/search by others
69
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]=Util.byte2str(buf.getString());
163           attr.extended[i*2+1]=Util.byte2str(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(Util.str2byte(extended[i*2]));
206           buf.putString(Util.str2byte(extended[i*2+1]));
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   /*
255   public String toString(){
256     return (((flags&SSH_FILEXFER_ATTR_SIZE)!=0) ? ("size:"+size+" ") : "")+
257            (((flags&SSH_FILEXFER_ATTR_UIDGID)!=0) ? ("uid:"+uid+",gid:"+gid+" ") : "")+
258            (((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0) ? ("permissions:0x"+Integer.toHexString(permissions)+" ") : "")+
259            (((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0) ? ("atime:"+atime+",mtime:"+mtime+" ") : "")+
260            (((flags&SSH_FILEXFER_ATTR_EXTENDED)!=0) ? ("extended:?"+" ") : "");
261   }
262   */
263 }
This page took 0.039188 seconds and 4 git commands to generate.