comparison src/ch/ethz/ssh2/SFTPv3FileAttributes.java @ 273:91a31873c42a ganymed

start conversion from trilead to ganymed
author Carl Byington <carl@five-ten-sg.com>
date Fri, 18 Jul 2014 11:21:46 -0700
parents
children d2b303406d63
comparison
equal deleted inserted replaced
272:ce2f4e397703 273:91a31873c42a
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2;
6
7 import java.io.IOException;
8
9 import ch.ethz.ssh2.packets.TypesReader;
10 import ch.ethz.ssh2.packets.TypesWriter;
11 import ch.ethz.ssh2.sftp.AttribFlags;
12
13 /**
14 * A <code>SFTPv3FileAttributes</code> object represents detail information
15 * about a file on the server. Not all fields may/must be present.
16 *
17 * @author Christian Plattner, plattner@inf.ethz.ch
18 * @version $Id: SFTPv3FileAttributes.java 133 2014-04-14 12:26:29Z dkocher@sudo.ch $
19 */
20 public class SFTPv3FileAttributes implements SFTPFileAttributes {
21 /**
22 * The SIZE attribute. <code>NULL</code> if not present.
23 */
24 public Long size = null;
25
26 /**
27 * The UID attribute. <code>NULL</code> if not present.
28 */
29 public Integer uid = null;
30
31 /**
32 * The GID attribute. <code>NULL</code> if not present.
33 */
34 public Integer gid = null;
35
36 /**
37 * The POSIX permissions. <code>NULL</code> if not present.
38 * <p/>
39 * Here is a list:
40 * <p/>
41 * <pre>Note: these numbers are all OCTAL.
42 * <p/>
43 * S_IFMT 0170000 bitmask for the file type bitfields
44 * S_IFSOCK 0140000 socket
45 * S_IFLNK 0120000 symbolic link
46 * S_IFREG 0100000 regular file
47 * S_IFBLK 0060000 block device
48 * S_IFDIR 0040000 directory
49 * S_IFCHR 0020000 character device
50 * S_IFIFO 0010000 fifo
51 * S_ISUID 0004000 set UID bit
52 * S_ISGID 0002000 set GID bit
53 * S_ISVTX 0001000 sticky bit
54 * <p/>
55 * S_IRWXU 00700 mask for file owner permissions
56 * S_IRUSR 00400 owner has read permission
57 * S_IWUSR 00200 owner has write permission
58 * S_IXUSR 00100 owner has execute permission
59 * S_IRWXG 00070 mask for group permissions
60 * S_IRGRP 00040 group has read permission
61 * S_IWGRP 00020 group has write permission
62 * S_IXGRP 00010 group has execute permission
63 * S_IRWXO 00007 mask for permissions for others (not in group)
64 * S_IROTH 00004 others have read permission
65 * S_IWOTH 00002 others have write permisson
66 * S_IXOTH 00001 others have execute permission
67 * </pre>
68 */
69 public Integer permissions = null;
70
71 /**
72 * Last access time of the file.
73 * <p/>
74 * The atime attribute. Represented as seconds from Jan 1, 1970 in UTC.
75 * <code>NULL</code> if not present.
76 */
77 public Integer atime = null;
78
79 /**
80 * The mtime attribute. Represented as seconds from Jan 1, 1970 in UTC.
81 * <code>NULL</code> if not present.
82 */
83 public Integer mtime = null;
84
85 /**
86 * Checks if this entry is a directory.
87 *
88 * @return Returns true if permissions are available and they indicate
89 * that this entry represents a directory.
90 */
91 @Override
92 public boolean isDirectory() {
93 if(permissions == null) {
94 return false;
95 }
96 return ((permissions & 0040000) == 0040000);
97 }
98
99 /**
100 * Checks if this entry is a regular file.
101 *
102 * @return Returns true if permissions are available and they indicate
103 * that this entry represents a regular file.
104 */
105 @Override
106 public boolean isRegularFile() {
107 if(permissions == null) {
108 return false;
109 }
110 return ((permissions & 0100000) == 0100000);
111 }
112
113 /**
114 * Checks if this entry is a a symlink.
115 *
116 * @return Returns true if permissions are available and they indicate
117 * that this entry represents a symlink.
118 */
119 @Override
120 public boolean isSymlink() {
121 if(permissions == null) {
122 return false;
123 }
124 return ((permissions & 0120000) == 0120000);
125 }
126
127 /**
128 * Turn the POSIX permissions into a 7 digit octal representation.
129 * Note: the returned value is first masked with <code>0177777</code>.
130 *
131 * @return <code>NULL</code> if permissions are not available.
132 */
133 public String getOctalPermissions() {
134 if(permissions == null) {
135 return null;
136 }
137 String res = Integer.toString(permissions.intValue() & 0177777, 8);
138
139 StringBuilder sb = new StringBuilder();
140
141 int leadingZeros = 7 - res.length();
142
143 while(leadingZeros > 0) {
144 sb.append('0');
145 leadingZeros--;
146 }
147
148 sb.append(res);
149
150 return sb.toString();
151 }
152
153 public SFTPv3FileAttributes() {
154 //
155 }
156
157 /**
158 * uint32 valid-attribute-flags
159 * byte type always present
160 * uint64 size if flag SIZE
161 * uint64 allocation-size if flag ALLOCATION_SIZE
162 * string owner if flag OWNERGROUP
163 * string group if flag OWNERGROUP
164 * uint32 permissions if flag PERMISSIONS
165 * int64 atime if flag ACCESSTIME
166 * uint32 atime-nseconds if flag SUBSECOND_TIMES
167 * int64 createtime if flag CREATETIME
168 * uint32 createtime-nseconds if flag SUBSECOND_TIMES
169 * int64 mtime if flag MODIFYTIME
170 * uint32 mtime-nseconds if flag SUBSECOND_TIMES
171 * int64 ctime if flag CTIME
172 * uint32 ctime-nseconds if flag SUBSECOND_TIMES
173 * string acl if flag ACL
174 * uint32 attrib-bits if flag BITS
175 * uint32 attrib-bits-valid if flag BITS
176 * byte text-hint if flag TEXT_HINT
177 * string mime-type if flag MIME_TYPE
178 * uint32 link-count if flag LINK_COUNT
179 * string untranslated-name if flag UNTRANSLATED_NAME
180 * uint32 extended-count if flag EXTENDED
181 * extension-pair extensions
182 */
183 public SFTPv3FileAttributes(final TypesReader tr) throws IOException {
184 int flags = tr.readUINT32();
185 if((flags & AttribFlags.SSH_FILEXFER_ATTR_SIZE) != 0) {
186 this.size = tr.readUINT64();
187 }
188 if((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID) != 0) {
189 this.uid = tr.readUINT32();
190 this.gid = tr.readUINT32();
191 }
192 if((flags & AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
193 this.permissions = tr.readUINT32();
194 }
195 if((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME) != 0) {
196 this.atime = tr.readUINT32();
197 this.mtime = tr.readUINT32();
198
199 }
200 if((flags & AttribFlags.SSH_FILEXFER_ATTR_EXTENDED) != 0) {
201 int count = tr.readUINT32();
202 // Read it anyway to detect corrupt packets
203 while(count > 0) {
204 tr.readByteString();
205 tr.readByteString();
206 count--;
207 }
208 }
209 }
210
211 /**
212 * The same encoding is used both when returning file
213 * attributes from the server and when sending file attributes to the
214 * server.
215 *
216 * @return Encoded attributes
217 */
218 @Override
219 public byte[] toBytes() {
220 TypesWriter tw = new TypesWriter();
221 int attrFlags = 0;
222 if(this.size != null) {
223 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE;
224 }
225 if((this.uid != null) && (this.gid != null)) {
226 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID;
227 }
228 if(this.permissions != null) {
229 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS;
230 }
231 if((this.atime != null) && (this.mtime != null)) {
232 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME;
233 }
234 tw.writeUINT32(attrFlags);
235 if(this.size != null) {
236 tw.writeUINT64(this.size);
237 }
238
239 if((this.uid != null) && (this.gid != null)) {
240 tw.writeUINT32(this.uid);
241 tw.writeUINT32(this.gid);
242 }
243
244 if(this.permissions != null) {
245 tw.writeUINT32(this.permissions);
246 }
247
248 if((this.atime != null) && (this.mtime != null)) {
249 tw.writeUINT32(this.atime);
250 tw.writeUINT32(this.mtime);
251 }
252 return tw.getBytes();
253 }
254
255 @Override
256 public String toString() {
257 final StringBuilder sb = new StringBuilder("SFTPv3FileAttributes{");
258 sb.append("size=").append(size);
259 sb.append(", uid=").append(uid);
260 sb.append(", gid=").append(gid);
261 sb.append(", permissions=").append(permissions);
262 sb.append(", atime=").append(atime);
263 sb.append(", mtime=").append(mtime);
264 sb.append('}');
265 return sb.toString();
266 }
267 }