Mercurial > 510Connectbot
comparison app/src/main/java/ch/ethz/ssh2/SFTPv6FileAttributes.java @ 438:d29cce60f393
migrate from Eclipse to Android Studio
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 03 Dec 2015 11:23:55 -0800 |
parents | src/ch/ethz/ssh2/SFTPv6FileAttributes.java@071eccdff8ea |
children |
comparison
equal
deleted
inserted
replaced
437:208b31032318 | 438:d29cce60f393 |
---|---|
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 public boolean isDirectory() { | |
119 return (type & AttribTypes.SSH_FILEXFER_TYPE_DIRECTORY) == AttribTypes.SSH_FILEXFER_TYPE_DIRECTORY; | |
120 } | |
121 | |
122 /** | |
123 * Checks if this entry is a regular file. | |
124 * | |
125 * @return Returns true if permissions are available and they indicate | |
126 * that this entry represents a regular file. | |
127 */ | |
128 public boolean isRegularFile() { | |
129 return (type & AttribTypes.SSH_FILEXFER_TYPE_REGULAR) == AttribTypes.SSH_FILEXFER_TYPE_REGULAR; | |
130 } | |
131 | |
132 /** | |
133 * Checks if this entry is a a symlink. | |
134 * | |
135 * @return Returns true if permissions are available and they indicate | |
136 * that this entry represents a symlink. | |
137 */ | |
138 public boolean isSymlink() { | |
139 return (type & AttribTypes.SSH_FILEXFER_TYPE_SYMLINK) == AttribTypes.SSH_FILEXFER_TYPE_SYMLINK; | |
140 } | |
141 | |
142 public SFTPv6FileAttributes() { | |
143 // | |
144 } | |
145 | |
146 /** | |
147 * uint32 valid-attribute-flags | |
148 * byte type always present | |
149 * uint64 size if flag SIZE | |
150 * uint64 allocation-size if flag ALLOCATION_SIZE | |
151 * string owner if flag OWNERGROUP | |
152 * string group if flag OWNERGROUP | |
153 * uint32 permissions if flag PERMISSIONS | |
154 * int64 atime if flag ACCESSTIME | |
155 * uint32 atime-nseconds if flag SUBSECOND_TIMES | |
156 * int64 createtime if flag CREATETIME | |
157 * uint32 createtime-nseconds if flag SUBSECOND_TIMES | |
158 * int64 mtime if flag MODIFYTIME | |
159 * uint32 mtime-nseconds if flag SUBSECOND_TIMES | |
160 * int64 ctime if flag CTIME | |
161 * uint32 ctime-nseconds if flag SUBSECOND_TIMES | |
162 * string acl if flag ACL | |
163 * uint32 attrib-bits if flag BITS | |
164 * uint32 attrib-bits-valid if flag BITS | |
165 * byte text-hint if flag TEXT_HINT | |
166 * string mime-type if flag MIME_TYPE | |
167 * uint32 link-count if flag LINK_COUNT | |
168 * string untranslated-name if flag UNTRANSLATED_NAME | |
169 * uint32 extended-count if flag EXTENDED | |
170 * extension-pair extensions | |
171 */ | |
172 public SFTPv6FileAttributes(final TypesReader tr) throws IOException { | |
173 int flags = tr.readUINT32(); | |
174 // The type field is always present | |
175 this.type = tr.readByte(); | |
176 | |
177 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_SIZE) != 0) { | |
178 this.size = tr.readUINT64(); | |
179 } | |
180 | |
181 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_ALLOCATION_SIZE) != 0) { | |
182 // Ignore | |
183 tr.readUINT64(); | |
184 } | |
185 | |
186 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_OWNERGROUP) != 0) { | |
187 this.owner = tr.readString(); | |
188 this.group = tr.readString(); | |
189 } | |
190 | |
191 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS) != 0) { | |
192 this.permissions = tr.readUINT32(); | |
193 } | |
194 | |
195 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_ACCESSTIME) != 0) { | |
196 this.atime = tr.readUINT64(); | |
197 } | |
198 | |
199 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) { | |
200 // Ignore | |
201 tr.readUINT32(); | |
202 } | |
203 | |
204 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_CREATETIME) != 0) { | |
205 this.createtime = tr.readUINT64(); | |
206 } | |
207 | |
208 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) { | |
209 // Ignore | |
210 tr.readUINT32(); | |
211 } | |
212 | |
213 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_MODIFYTIME) != 0) { | |
214 this.mtime = tr.readUINT64(); | |
215 } | |
216 | |
217 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) { | |
218 // Ignore | |
219 tr.readUINT32(); | |
220 } | |
221 | |
222 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_CTIME) != 0) { | |
223 this.ctime = tr.readUINT64(); | |
224 } | |
225 | |
226 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_SUBSECOND_TIMES) != 0) { | |
227 // Ignore | |
228 tr.readUINT32(); | |
229 } | |
230 | |
231 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_ACL) != 0) { | |
232 // Ignore | |
233 tr.readString(); | |
234 } | |
235 | |
236 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_BITS) != 0) { | |
237 // Ignore attrib-bits | |
238 tr.readUINT32(); | |
239 // Ignore attrib-bits-valid | |
240 tr.readUINT32(); | |
241 } | |
242 | |
243 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_TEXT_HINT) != 0) { | |
244 // Ignore | |
245 tr.readByte(); | |
246 } | |
247 | |
248 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_MIME_TYPE) != 0) { | |
249 // Ignore | |
250 tr.readString(); | |
251 } | |
252 | |
253 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_LINK_COUNT) != 0) { | |
254 // Ignore | |
255 tr.readUINT32(); | |
256 } | |
257 | |
258 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_UNTRANSLATED_NAME) != 0) { | |
259 // Ignore | |
260 tr.readString(); | |
261 } | |
262 | |
263 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_EXTENDED) != 0) { | |
264 int count = tr.readUINT32(); | |
265 | |
266 // Read it anyway to detect corrupt packets | |
267 while (count > 0) { | |
268 // extension-name | |
269 tr.readByteString(); | |
270 // extension-data | |
271 tr.readByteString(); | |
272 count--; | |
273 } | |
274 } | |
275 } | |
276 | |
277 /** | |
278 * The same encoding is used both when returning file | |
279 * attributes from the server and when sending file attributes to the | |
280 * server. | |
281 * | |
282 * @return Encoded attributes | |
283 */ | |
284 public byte[] toBytes() { | |
285 TypesWriter tw = new TypesWriter(); | |
286 // The 'valid-attribute-flags' specifies which of the fields are present. Those fields | |
287 // for which the corresponding flag is not set are not present | |
288 int attrFlags = 0; | |
289 | |
290 if (this.size != null) { | |
291 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE; | |
292 } | |
293 | |
294 if ((this.owner != null) && (this.group != null)) { | |
295 // If either the owner or group field is zero length, the field should | |
296 // be considered absent, and no change should be made to that specific | |
297 // field during a modification operation. | |
298 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_OWNERGROUP; | |
299 } | |
300 | |
301 if (this.permissions != null) { | |
302 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS; | |
303 } | |
304 | |
305 if (this.atime != null) { | |
306 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_ACCESSTIME; | |
307 } | |
308 | |
309 if (this.createtime != null) { | |
310 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_CREATETIME; | |
311 } | |
312 | |
313 if (this.mtime != null) { | |
314 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_MODIFYTIME; | |
315 } | |
316 | |
317 if (this.ctime != null) { | |
318 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_CTIME; | |
319 } | |
320 | |
321 tw.writeUINT32(attrFlags); | |
322 | |
323 // The type field is always present. | |
324 if (this.size != null) { | |
325 tw.writeUINT64(this.size); | |
326 } | |
327 | |
328 if ((this.owner != null) && (this.group != null)) { | |
329 tw.writeString(owner); | |
330 tw.writeString(group); | |
331 } | |
332 | |
333 if (this.permissions != null) { | |
334 tw.writeUINT32(this.permissions); | |
335 } | |
336 | |
337 if (this.atime != null) { | |
338 tw.writeUINT64(this.atime); | |
339 } | |
340 | |
341 if (this.createtime != null) { | |
342 tw.writeUINT64(this.createtime); | |
343 } | |
344 | |
345 if (this.mtime != null) { | |
346 tw.writeUINT64(this.mtime); | |
347 } | |
348 | |
349 if (this.ctime != null) { | |
350 tw.writeUINT64(this.ctime); | |
351 } | |
352 | |
353 return tw.getBytes(); | |
354 } | |
355 | |
356 @Override | |
357 public String toString() { | |
358 final StringBuilder sb = new StringBuilder("SFTPv6FileAttributes{"); | |
359 sb.append("type=").append(type); | |
360 sb.append(", size=").append(size); | |
361 sb.append(", permissions=").append(permissions); | |
362 sb.append(", createtime=").append(createtime); | |
363 sb.append(", atime=").append(atime); | |
364 sb.append(", mtime=").append(mtime); | |
365 sb.append(", ctime=").append(ctime); | |
366 sb.append(", owner='").append(owner).append('\''); | |
367 sb.append(", group='").append(group).append('\''); | |
368 sb.append('}'); | |
369 return sb.toString(); | |
370 } | |
371 } |