1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
3 Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
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.
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.
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.
30 package com.jcraft.jsch;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
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
46 ... more extended data (extended_type - extended_data pairs),
47 so that number of pairs equals extended_count
49 public class SftpATTRS {
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 *****
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
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
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
70 private static final int pmask = 0xFFF;
72 public String getPermissionsString() {
73 StringBuffer buf = new StringBuffer(10);
75 if(isDir()) buf.append('d');
76 else if(isLink()) buf.append('l');
79 if((permissions & S_IRUSR)!=0) buf.append('r');
82 if((permissions & S_IWUSR)!=0) buf.append('w');
85 if((permissions & S_ISUID)!=0) buf.append('s');
86 else if ((permissions & S_IXUSR)!=0) buf.append('x');
89 if((permissions & S_IRGRP)!=0) buf.append('r');
92 if((permissions & S_IWGRP)!=0) buf.append('w');
95 if((permissions & S_ISGID)!=0) buf.append('s');
96 else if((permissions & S_IXGRP)!=0) buf.append('x');
99 if((permissions & S_IROTH) != 0) buf.append('r');
100 else buf.append('-');
102 if((permissions & S_IWOTH) != 0) buf.append('w');
103 else buf.append('-');
105 if((permissions & S_IXOTH) != 0) buf.append('x');
106 else buf.append('-');
107 return (buf.toString());
110 public String getAtimeString(){
111 SimpleDateFormat locale=new SimpleDateFormat();
112 return (locale.format(new Date(atime)));
115 public String getMtimeString(){
116 Date date= new Date(((long)mtime)*1000);
117 return (date.toString());
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;
126 static final int S_IFDIR=0x4000;
127 static final int S_IFLNK=0xa000;
136 String[] extended=null;
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();
148 if((attr.flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){
149 attr.permissions=buf.getInt();
151 if((attr.flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){
152 attr.atime=buf.getInt();
154 if((attr.flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0){
155 attr.mtime=buf.getInt();
157 if((attr.flags&SSH_FILEXFER_ATTR_EXTENDED)!=0){
158 int count=buf.getInt();
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());
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){
179 int count=extended.length/2;
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();
190 void dump(Buffer buf){
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);
196 if((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0){
197 buf.putInt(permissions);
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;
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]));
211 void setFLAGS(int flags){
214 public void setSIZE(long size){
215 flags|=SSH_FILEXFER_ATTR_SIZE;
218 public void setUIDGID(int uid, int gid){
219 flags|=SSH_FILEXFER_ATTR_UIDGID;
223 public void setACMODTIME(int atime, int mtime){
224 flags|=SSH_FILEXFER_ATTR_ACMODTIME;
228 public void setPERMISSIONS(int permissions){
229 flags|=SSH_FILEXFER_ATTR_PERMISSIONS;
230 permissions=(this.permissions&~pmask)|(permissions&pmask);
231 this.permissions=permissions;
234 public boolean isDir(){
235 return ((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0 &&
236 ((permissions&S_IFDIR)==S_IFDIR));
238 public boolean isLink(){
239 return ((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0 &&
240 ((permissions&S_IFLNK)==S_IFLNK));
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; }
251 public String toString() {
252 return (getPermissionsString()+" "+getUId()+" "+getGId()+" "+getSize()+" "+getMtimeString());
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:?"+" ") : "");