comparison app/src/main/java/ch/ethz/ssh2/SFTPv3FileAttributes.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/SFTPv3FileAttributes.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
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 public boolean isDirectory() {
92 if (permissions == null) {
93 return false;
94 }
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 public boolean isRegularFile() {
106 if (permissions == null) {
107 return false;
108 }
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 public boolean isSymlink() {
120 if (permissions == null) {
121 return false;
122 }
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
138 String res = Integer.toString(permissions.intValue() & 0177777, 8);
139 StringBuilder sb = new StringBuilder();
140 int leadingZeros = 7 - res.length();
141
142 while (leadingZeros > 0) {
143 sb.append('0');
144 leadingZeros--;
145 }
146
147 sb.append(res);
148 return sb.toString();
149 }
150
151 public SFTPv3FileAttributes() {
152 //
153 }
154
155 /**
156 * uint32 valid-attribute-flags
157 * byte type always present
158 * uint64 size if flag SIZE
159 * uint64 allocation-size if flag ALLOCATION_SIZE
160 * string owner if flag OWNERGROUP
161 * string group if flag OWNERGROUP
162 * uint32 permissions if flag PERMISSIONS
163 * int64 atime if flag ACCESSTIME
164 * uint32 atime-nseconds if flag SUBSECOND_TIMES
165 * int64 createtime if flag CREATETIME
166 * uint32 createtime-nseconds if flag SUBSECOND_TIMES
167 * int64 mtime if flag MODIFYTIME
168 * uint32 mtime-nseconds if flag SUBSECOND_TIMES
169 * int64 ctime if flag CTIME
170 * uint32 ctime-nseconds if flag SUBSECOND_TIMES
171 * string acl if flag ACL
172 * uint32 attrib-bits if flag BITS
173 * uint32 attrib-bits-valid if flag BITS
174 * byte text-hint if flag TEXT_HINT
175 * string mime-type if flag MIME_TYPE
176 * uint32 link-count if flag LINK_COUNT
177 * string untranslated-name if flag UNTRANSLATED_NAME
178 * uint32 extended-count if flag EXTENDED
179 * extension-pair extensions
180 */
181 public SFTPv3FileAttributes(final TypesReader tr) throws IOException {
182 int flags = tr.readUINT32();
183
184 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_SIZE) != 0) {
185 this.size = tr.readUINT64();
186 }
187
188 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID) != 0) {
189 this.uid = tr.readUINT32();
190 this.gid = tr.readUINT32();
191 }
192
193 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
194 this.permissions = tr.readUINT32();
195 }
196
197 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME) != 0) {
198 this.atime = tr.readUINT32();
199 this.mtime = tr.readUINT32();
200 }
201
202 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_EXTENDED) != 0) {
203 int count = tr.readUINT32();
204
205 // Read it anyway to detect corrupt packets
206 while (count > 0) {
207 tr.readByteString();
208 tr.readByteString();
209 count--;
210 }
211 }
212 }
213
214 /**
215 * The same encoding is used both when returning file
216 * attributes from the server and when sending file attributes to the
217 * server.
218 *
219 * @return Encoded attributes
220 */
221 public byte[] toBytes() {
222 TypesWriter tw = new TypesWriter();
223 int attrFlags = 0;
224
225 if (this.size != null) {
226 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE;
227 }
228
229 if ((this.uid != null) && (this.gid != null)) {
230 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID;
231 }
232
233 if (this.permissions != null) {
234 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS;
235 }
236
237 if ((this.atime != null) && (this.mtime != null)) {
238 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME;
239 }
240
241 tw.writeUINT32(attrFlags);
242
243 if (this.size != null) {
244 tw.writeUINT64(this.size);
245 }
246
247 if ((this.uid != null) && (this.gid != null)) {
248 tw.writeUINT32(this.uid);
249 tw.writeUINT32(this.gid);
250 }
251
252 if (this.permissions != null) {
253 tw.writeUINT32(this.permissions);
254 }
255
256 if ((this.atime != null) && (this.mtime != null)) {
257 tw.writeUINT32(this.atime);
258 tw.writeUINT32(this.mtime);
259 }
260
261 return tw.getBytes();
262 }
263
264 @Override
265 public String toString() {
266 final StringBuilder sb = new StringBuilder("SFTPv3FileAttributes{");
267 sb.append("size=").append(size);
268 sb.append(", uid=").append(uid);
269 sb.append(", gid=").append(gid);
270 sb.append(", permissions=").append(permissions);
271 sb.append(", atime=").append(atime);
272 sb.append(", mtime=").append(mtime);
273 sb.append('}');
274 return sb.toString();
275 }
276 }