001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch;
031:
032: import java.text.SimpleDateFormat;
033: import java.util.Date;
034:
035: /*
036: uint32 flags
037: uint64 size present only if flag SSH_FILEXFER_ATTR_SIZE
038: uint32 uid present only if flag SSH_FILEXFER_ATTR_UIDGID
039: uint32 gid present only if flag SSH_FILEXFER_ATTR_UIDGID
040: uint32 permissions present only if flag SSH_FILEXFER_ATTR_PERMISSIONS
041: uint32 atime present only if flag SSH_FILEXFER_ACMODTIME
042: uint32 mtime present only if flag SSH_FILEXFER_ACMODTIME
043: uint32 extended_count present only if flag SSH_FILEXFER_ATTR_EXTENDED
044: string extended_type
045: string extended_data
046: ... more extended data (extended_type - extended_data pairs),
047: so that number of pairs equals extended_count
048: */
049: public class SftpATTRS {
050:
051: static final int S_ISUID = 04000; // set user ID on execution
052: static final int S_ISGID = 02000; // set group ID on execution
053: static final int S_ISVTX = 01000; // sticky bit ****** NOT DOCUMENTED *****
054:
055: static final int S_IRUSR = 00400; // read by owner
056: static final int S_IWUSR = 00200; // write by owner
057: static final int S_IXUSR = 00100; // execute/search by owner
058: static final int S_IREAD = 00400; // read by owner
059: static final int S_IWRITE = 00200; // write by owner
060: static final int S_IEXEC = 00100; // execute/search by owner
061:
062: static final int S_IRGRP = 00040; // read by group
063: static final int S_IWGRP = 00020; // write by group
064: static final int S_IXGRP = 00010; // execute/search by group
065:
066: static final int S_IROTH = 00004; // read by others
067: static final int S_IWOTH = 00002; // write by others
068: static final int S_IXOTH = 00001; // execute/search by others
069:
070: private static final int pmask = 0xFFF;
071:
072: public String getPermissionsString() {
073: StringBuffer buf = new StringBuffer(10);
074:
075: if (isDir())
076: buf.append('d');
077: else if (isLink())
078: buf.append('l');
079: else
080: buf.append('-');
081:
082: if ((permissions & S_IRUSR) != 0)
083: buf.append('r');
084: else
085: buf.append('-');
086:
087: if ((permissions & S_IWUSR) != 0)
088: buf.append('w');
089: else
090: buf.append('-');
091:
092: if ((permissions & S_ISUID) != 0)
093: buf.append('s');
094: else if ((permissions & S_IXUSR) != 0)
095: buf.append('x');
096: else
097: buf.append('-');
098:
099: if ((permissions & S_IRGRP) != 0)
100: buf.append('r');
101: else
102: buf.append('-');
103:
104: if ((permissions & S_IWGRP) != 0)
105: buf.append('w');
106: else
107: buf.append('-');
108:
109: if ((permissions & S_ISGID) != 0)
110: buf.append('s');
111: else if ((permissions & S_IXGRP) != 0)
112: buf.append('x');
113: else
114: buf.append('-');
115:
116: if ((permissions & S_IROTH) != 0)
117: buf.append('r');
118: else
119: buf.append('-');
120:
121: if ((permissions & S_IWOTH) != 0)
122: buf.append('w');
123: else
124: buf.append('-');
125:
126: if ((permissions & S_IXOTH) != 0)
127: buf.append('x');
128: else
129: buf.append('-');
130: return (buf.toString());
131: }
132:
133: public String getAtimeString() {
134: SimpleDateFormat locale = new SimpleDateFormat();
135: return (locale.format(new Date(atime)));
136: }
137:
138: public String getMtimeString() {
139: Date date = new Date(((long) mtime) * 1000);
140: return (date.toString());
141: }
142:
143: public static final int SSH_FILEXFER_ATTR_SIZE = 0x00000001;
144: public static final int SSH_FILEXFER_ATTR_UIDGID = 0x00000002;
145: public static final int SSH_FILEXFER_ATTR_PERMISSIONS = 0x00000004;
146: public static final int SSH_FILEXFER_ATTR_ACMODTIME = 0x00000008;
147: public static final int SSH_FILEXFER_ATTR_EXTENDED = 0x80000000;
148:
149: static final int S_IFDIR = 0x4000;
150: static final int S_IFLNK = 0xa000;
151:
152: int flags = 0;
153: long size;
154: int uid;
155: int gid;
156: int permissions;
157: int atime;
158: int mtime;
159: String[] extended = null;
160:
161: private SftpATTRS() {
162: }
163:
164: static SftpATTRS getATTR(Buffer buf) {
165: SftpATTRS attr = new SftpATTRS();
166: attr.flags = buf.getInt();
167: if ((attr.flags & SSH_FILEXFER_ATTR_SIZE) != 0) {
168: attr.size = buf.getLong();
169: }
170: if ((attr.flags & SSH_FILEXFER_ATTR_UIDGID) != 0) {
171: attr.uid = buf.getInt();
172: attr.gid = buf.getInt();
173: }
174: if ((attr.flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
175: attr.permissions = buf.getInt();
176: }
177: if ((attr.flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) {
178: attr.atime = buf.getInt();
179: }
180: if ((attr.flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) {
181: attr.mtime = buf.getInt();
182: }
183: if ((attr.flags & SSH_FILEXFER_ATTR_EXTENDED) != 0) {
184: int count = buf.getInt();
185: if (count > 0) {
186: attr.extended = new String[count * 2];
187: for (int i = 0; i < count; i++) {
188: attr.extended[i * 2] = new String(buf.getString());
189: attr.extended[i * 2 + 1] = new String(buf
190: .getString());
191: }
192: }
193: }
194: return attr;
195: }
196:
197: int length() {
198: int len = 4;
199:
200: if ((flags & SSH_FILEXFER_ATTR_SIZE) != 0) {
201: len += 8;
202: }
203: if ((flags & SSH_FILEXFER_ATTR_UIDGID) != 0) {
204: len += 8;
205: }
206: if ((flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
207: len += 4;
208: }
209: if ((flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) {
210: len += 8;
211: }
212: if ((flags & SSH_FILEXFER_ATTR_EXTENDED) != 0) {
213: len += 4;
214: int count = extended.length / 2;
215: if (count > 0) {
216: for (int i = 0; i < count; i++) {
217: len += 4;
218: len += extended[i * 2].length();
219: len += 4;
220: len += extended[i * 2 + 1].length();
221: }
222: }
223: }
224: return len;
225: }
226:
227: void dump(Buffer buf) {
228: buf.putInt(flags);
229: if ((flags & SSH_FILEXFER_ATTR_SIZE) != 0) {
230: buf.putLong(size);
231: }
232: if ((flags & SSH_FILEXFER_ATTR_UIDGID) != 0) {
233: buf.putInt(uid);
234: buf.putInt(gid);
235: }
236: if ((flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
237: buf.putInt(permissions);
238: }
239: if ((flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) {
240: buf.putInt(atime);
241: }
242: if ((flags & SSH_FILEXFER_ATTR_ACMODTIME) != 0) {
243: buf.putInt(mtime);
244: }
245: if ((flags & SSH_FILEXFER_ATTR_EXTENDED) != 0) {
246: int count = extended.length / 2;
247: if (count > 0) {
248: for (int i = 0; i < count; i++) {
249: buf.putString(extended[i * 2].getBytes());
250: buf.putString(extended[i * 2 + 1].getBytes());
251: }
252: }
253: }
254: }
255:
256: void setFLAGS(int flags) {
257: this .flags = flags;
258: }
259:
260: public void setSIZE(long size) {
261: flags |= SSH_FILEXFER_ATTR_SIZE;
262: this .size = size;
263: }
264:
265: public void setUIDGID(int uid, int gid) {
266: flags |= SSH_FILEXFER_ATTR_UIDGID;
267: this .uid = uid;
268: this .gid = gid;
269: }
270:
271: public void setACMODTIME(int atime, int mtime) {
272: flags |= SSH_FILEXFER_ATTR_ACMODTIME;
273: this .atime = atime;
274: this .mtime = mtime;
275: }
276:
277: public void setPERMISSIONS(int permissions) {
278: flags |= SSH_FILEXFER_ATTR_PERMISSIONS;
279: permissions = (this .permissions & ~pmask)
280: | (permissions & pmask);
281: this .permissions = permissions;
282: }
283:
284: public boolean isDir() {
285: return ((flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0 && ((permissions & S_IFDIR) == S_IFDIR));
286: }
287:
288: public boolean isLink() {
289: return ((flags & SSH_FILEXFER_ATTR_PERMISSIONS) != 0 && ((permissions & S_IFLNK) == S_IFLNK));
290: }
291:
292: public int getFlags() {
293: return flags;
294: }
295:
296: public long getSize() {
297: return size;
298: }
299:
300: public int getUId() {
301: return uid;
302: }
303:
304: public int getGId() {
305: return gid;
306: }
307:
308: public int getPermissions() {
309: return permissions;
310: }
311:
312: public int getATime() {
313: return atime;
314: }
315:
316: public int getMTime() {
317: return mtime;
318: }
319:
320: public String[] getExtended() {
321: return extended;
322: }
323:
324: public String toString() {
325: return (getPermissionsString() + " " + getUId() + " "
326: + getGId() + " " + getSize() + " " + getMtimeString());
327: }
328: /*
329: public String toString(){
330: return (((flags&SSH_FILEXFER_ATTR_SIZE)!=0) ? ("size:"+size+" ") : "")+
331: (((flags&SSH_FILEXFER_ATTR_UIDGID)!=0) ? ("uid:"+uid+",gid:"+gid+" ") : "")+
332: (((flags&SSH_FILEXFER_ATTR_PERMISSIONS)!=0) ? ("permissions:0x"+Integer.toHexString(permissions)+" ") : "")+
333: (((flags&SSH_FILEXFER_ATTR_ACMODTIME)!=0) ? ("atime:"+atime+",mtime:"+mtime+" ") : "")+
334: (((flags&SSH_FILEXFER_ATTR_EXTENDED)!=0) ? ("extended:?"+" ") : "");
335: }
336: */
337: }
|