comparison src/ch/ethz/ssh2/SFTPv6FileAttributes.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 import ch.ethz.ssh2.sftp.AttribTypes;
13
14 /**
15 * A <code>SFTPv3FileAttributes</code> object represents detail information
16 * about a file on the server. Not all fields may/must be present.
17 *
18 * @author Christian Plattner, plattner@inf.ethz.ch
19 * @version $Id: SFTPv6FileAttributes.java 133 2014-04-14 12:26:29Z dkocher@sudo.ch $
20 */
21
22 public class SFTPv6FileAttributes implements SFTPFileAttributes {
23
24 /**
25 * The type field is always present
26 *
27 * @see ch.ethz.ssh2.sftp.AttribTypes
28 */
29 private Integer type = null;
30
31 /**
32 * The SIZE attribute. <code>NULL</code> if not present.
33 */
34 public Long size = 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 * Creation time of the file.
73 * <p/>
74 * The createtime attribute. Represented as seconds from Jan 1, 1970 in UTC.
75 * <code>NULL</code> if not present.
76 */
77 public Long createtime = null;
78
79 /**
80 * Last access time of the file.
81 * <p/>
82 * The atime attribute. Represented as seconds from Jan 1, 1970 in UTC.
83 * <code>NULL</code> if not present.
84 */
85 public Long atime = null;
86
87 /**
88 * The mtime attribute. Represented as seconds from Jan 1, 1970 in UTC.
89 * <code>NULL</code> if not present.
90 */
91 public Long mtime = null;
92
93 /**
94 * Last time the file attributes were changed. The exact meaning of this field depends on the server.
95 * <p/>
96 * The ctime attribute. Represented as seconds from Jan 1, 1970 in UTC.
97 * <code>NULL</code> if not present.
98 */
99 public Long ctime = null;
100
101 /**
102 * The 'owner' and 'group' fields are represented as UTF-8 strings. user@localhost represents
103 * a user in the context of the server.
104 */
105 public String owner = null;
106
107 /**
108 * The 'owner' and 'group' fields are represented as UTF-8 strings
109 */
110 public String group = null;
111
112 /**
113 * Checks if this entry is a directory.
114 *
115 * @return Returns true if permissions are available and they indicate
116 * that this entry represents a directory.
117 */
118 @Override
119 public boolean isDirectory() {
120 return (type & AttribTypes.SSH_FILEXFER_TYPE_DIRECTORY) == AttribTypes.SSH_FILEXFER_TYPE_DIRECTORY;
121 }
122
123 /**
124 * Checks if this entry is a regular file.
125 *
126 * @return Returns true if permissions are available and they indicate
127 * that this entry represents a regular file.
128 */
129 @Override
130 public boolean isRegularFile() {
131 return (type & AttribTypes.SSH_FILEXFER_TYPE_REGULAR) == AttribTypes.SSH_FILEXFER_TYPE_REGULAR;
132 }
133
134 /**
135 * Checks if this entry is a a symlink.
136 *
137 * @return Returns true if permissions are available and they indicate
138 * that this entry represents a symlink.
139 */
140 @Override
141 public boolean isSymlink() {
142 return (type & AttribTypes.SSH_FILEXFER_TYPE_SYMLINK) == AttribTypes.SSH_FILEXFER_TYPE_SYMLINK;
143 }
144
145 public SFTPv6FileAttributes() {
146 //
147 }
148
149 /**
150 * uint32 valid-attribute-flags
151 * byte type always present
152 * uint64 size if flag SIZE
153 * uint64 allocation-size if flag ALLOCATION_SIZE
154 * string owner if flag OWNERGROUP
155 * string group if flag OWNERGROUP
156 * uint32 permissions if flag PERMISSIONS
157 * int64 atime if flag ACCESSTIME
158 * uint32 atime-nseconds if flag SUBSECOND_TIMES
159 * int64 createtime if flag CREATETIME
160 * uint32 createtime-nseconds if flag SUBSECOND_TIMES
161 * int64 mtime if flag MODIFYTIME
162 * uint32 mtime-nseconds if flag SUBSECOND_TIMES
163 * int64 ctime if flag CTIME
164 * uint32 ctime-nseconds if flag SUBSECOND_TIMES
165 * string acl if flag ACL
166 * uint32 attrib-bits if flag BITS
167 * uint32 attrib-bits-valid if flag BITS
168 * byte text-hint if flag TEXT_HINT
169 * string mime-type if flag MIME_TYPE
170 * uint32 link-count if flag LINK_COUNT
171 * string untranslated-name if flag UNTRANSLATED_NAME
172 * uint32 extended-count if flag EXTENDED
173 * extension-pair extensions
174 */
175 public SFTPv6FileAttributes(final TypesReader tr) throws IOException {
176 int flags = tr.readUINT32();
177 // The type field is always present
178 this.type = tr.readByte();
179 if((flags & AttribFlags.SSH_FILEXFER_ATTR_SIZE) != 0) {
180 this.size = tr.readUINT64();
181 }
182 if((flags & AttribFlags.SSH_FILEXFER_ATTR_ALLOCATION_SIZE) != 0) {
183 // Ignore
184 tr.readUINT64();
185 }
186 if((flags & AttribFlags.SSH_FILEXFER_ATTR_OWNERGROUP) != 0) {
187 this.owner = tr.readString();
188 this.group = tr.readString();
189 }
190 if((flags & AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
191 this.permissions = tr.readUINT32();
192 }
193 if((flags & AttribFlags.SSH_FILEXFER_ATTR_ACCESSTIME) != 0) {
194 this.atime = tr.readUINT64();
195 }
196 if((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) {
197 // Ignore
198 tr.readUINT32();
199 }
200 if((flags & AttribFlags.SSH_FILEXFER_ATTR_CREATETIME) != 0) {
201 this.createtime = tr.readUINT64();
202 }
203 if((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) {
204 // Ignore
205 tr.readUINT32();
206 }
207 if((flags & AttribFlags.SSH_FILEXFER_ATTR_MODIFYTIME) != 0) {
208 this.mtime = tr.readUINT64();
209 }
210 if((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) {
211 // Ignore
212 tr.readUINT32();
213 }
214 if((flags & AttribFlags.SSH_FILEXFER_ATTR_CTIME) != 0) {
215 this.ctime = tr.readUINT64();
216 }
217 if((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) {
218 // Ignore
219 tr.readUINT32();
220 }
221 if((flags & AttribFlags.SSH_FILEXFER_ATTR_ACL) != 0) {
222 // Ignore
223 tr.readString();
224 }
225 if((flags & AttribFlags.SSH_FILEXFER_ATTR_BITS) != 0) {
226 // Ignore attrib-bits
227 tr.readUINT32();
228 // Ignore attrib-bits-valid
229 tr.readUINT32();
230 }
231 if((flags & AttribFlags.SSH_FILEXFER_ATTR_TEXT_HINT) != 0) {
232 // Ignore
233 tr.readByte();
234 }
235 if((flags & AttribFlags.SSH_FILEXFER_ATTR_MIME_TYPE) != 0) {
236 // Ignore
237 tr.readString();
238 }
239 if((flags & AttribFlags.SSH_FILEXFER_ATTR_LINK_COUNT) != 0) {
240 // Ignore
241 tr.readUINT32();
242 }
243 if((flags & AttribFlags.SSH_FILEXFER_ATTR_UNTRANSLATED_NAME) != 0) {
244 // Ignore
245 tr.readString();
246 }
247 if((flags & AttribFlags.SSH_FILEXFER_ATTR_EXTENDED) != 0) {
248 int count = tr.readUINT32();
249 // Read it anyway to detect corrupt packets
250 while(count > 0) {
251 // extension-name
252 tr.readByteString();
253 // extension-data
254 tr.readByteString();
255 count--;
256 }
257 }
258 }
259
260 /**
261 * The same encoding is used both when returning file
262 * attributes from the server and when sending file attributes to the
263 * server.
264 *
265 * @return Encoded attributes
266 */
267 @Override
268 public byte[] toBytes() {
269 TypesWriter tw = new TypesWriter();
270 // The 'valid-attribute-flags' specifies which of the fields are present. Those fields
271 // for which the corresponding flag is not set are not present
272 int attrFlags = 0;
273 if(this.size != null) {
274 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE;
275 }
276 if((this.owner != null) && (this.group != null)) {
277 // If either the owner or group field is zero length, the field should
278 // be considered absent, and no change should be made to that specific
279 // field during a modification operation.
280 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_OWNERGROUP;
281 }
282 if(this.permissions != null) {
283 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS;
284 }
285 if(this.atime != null) {
286 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_ACCESSTIME;
287 }
288 if(this.createtime != null) {
289 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_CREATETIME;
290 }
291 if(this.mtime != null) {
292 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_MODIFYTIME;
293 }
294 if(this.ctime != null) {
295 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_CTIME;
296 }
297 tw.writeUINT32(attrFlags);
298 // The type field is always present.
299 if(this.size != null) {
300 tw.writeUINT64(this.size);
301 }
302 if((this.owner != null) && (this.group != null)) {
303 tw.writeString(owner);
304 tw.writeString(group);
305 }
306 if(this.permissions != null) {
307 tw.writeUINT32(this.permissions);
308 }
309 if(this.atime != null) {
310 tw.writeUINT64(this.atime);
311 }
312 if(this.createtime != null) {
313 tw.writeUINT64(this.createtime);
314 }
315 if(this.mtime != null) {
316 tw.writeUINT64(this.mtime);
317 }
318 if(this.ctime != null) {
319 tw.writeUINT64(this.ctime);
320 }
321 return tw.getBytes();
322 }
323
324 @Override
325 public String toString() {
326 final StringBuilder sb = new StringBuilder("SFTPv6FileAttributes{");
327 sb.append("type=").append(type);
328 sb.append(", size=").append(size);
329 sb.append(", permissions=").append(permissions);
330 sb.append(", createtime=").append(createtime);
331 sb.append(", atime=").append(atime);
332 sb.append(", mtime=").append(mtime);
333 sb.append(", ctime=").append(ctime);
334 sb.append(", owner='").append(owner).append('\'');
335 sb.append(", group='").append(group).append('\'');
336 sb.append('}');
337 return sb.toString();
338 }
339 }