comparison src/ch/ethz/ssh2/SFTPv3FileAttributes.java @ 308:42b15aaa7ac7 ganymed

merge
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:21:50 -0700
parents 071eccdff8ea
children
comparison
equal deleted inserted replaced
306:90e47d99ea54 308:42b15aaa7ac7
87 * 87 *
88 * @return Returns true if permissions are available and they indicate 88 * @return Returns true if permissions are available and they indicate
89 * that this entry represents a directory. 89 * that this entry represents a directory.
90 */ 90 */
91 public boolean isDirectory() { 91 public boolean isDirectory() {
92 if(permissions == null) { 92 if (permissions == null) {
93 return false; 93 return false;
94 } 94 }
95
95 return ((permissions & 0040000) == 0040000); 96 return ((permissions & 0040000) == 0040000);
96 } 97 }
97 98
98 /** 99 /**
99 * Checks if this entry is a regular file. 100 * Checks if this entry is a regular file.
100 * 101 *
101 * @return Returns true if permissions are available and they indicate 102 * @return Returns true if permissions are available and they indicate
102 * that this entry represents a regular file. 103 * that this entry represents a regular file.
103 */ 104 */
104 public boolean isRegularFile() { 105 public boolean isRegularFile() {
105 if(permissions == null) { 106 if (permissions == null) {
106 return false; 107 return false;
107 } 108 }
109
108 return ((permissions & 0100000) == 0100000); 110 return ((permissions & 0100000) == 0100000);
109 } 111 }
110 112
111 /** 113 /**
112 * Checks if this entry is a a symlink. 114 * Checks if this entry is a a symlink.
113 * 115 *
114 * @return Returns true if permissions are available and they indicate 116 * @return Returns true if permissions are available and they indicate
115 * that this entry represents a symlink. 117 * that this entry represents a symlink.
116 */ 118 */
117 public boolean isSymlink() { 119 public boolean isSymlink() {
118 if(permissions == null) { 120 if (permissions == null) {
119 return false; 121 return false;
120 } 122 }
123
121 return ((permissions & 0120000) == 0120000); 124 return ((permissions & 0120000) == 0120000);
122 } 125 }
123 126
124 /** 127 /**
125 * Turn the POSIX permissions into a 7 digit octal representation. 128 * Turn the POSIX permissions into a 7 digit octal representation.
126 * Note: the returned value is first masked with <code>0177777</code>. 129 * Note: the returned value is first masked with <code>0177777</code>.
127 * 130 *
128 * @return <code>NULL</code> if permissions are not available. 131 * @return <code>NULL</code> if permissions are not available.
129 */ 132 */
130 public String getOctalPermissions() { 133 public String getOctalPermissions() {
131 if(permissions == null) { 134 if (permissions == null) {
132 return null; 135 return null;
133 } 136 }
137
134 String res = Integer.toString(permissions.intValue() & 0177777, 8); 138 String res = Integer.toString(permissions.intValue() & 0177777, 8);
135
136 StringBuilder sb = new StringBuilder(); 139 StringBuilder sb = new StringBuilder();
137
138 int leadingZeros = 7 - res.length(); 140 int leadingZeros = 7 - res.length();
139 141
140 while(leadingZeros > 0) { 142 while (leadingZeros > 0) {
141 sb.append('0'); 143 sb.append('0');
142 leadingZeros--; 144 leadingZeros--;
143 } 145 }
144 146
145 sb.append(res); 147 sb.append(res);
146
147 return sb.toString(); 148 return sb.toString();
148 } 149 }
149 150
150 public SFTPv3FileAttributes() { 151 public SFTPv3FileAttributes() {
151 // 152 //
177 * uint32 extended-count if flag EXTENDED 178 * uint32 extended-count if flag EXTENDED
178 * extension-pair extensions 179 * extension-pair extensions
179 */ 180 */
180 public SFTPv3FileAttributes(final TypesReader tr) throws IOException { 181 public SFTPv3FileAttributes(final TypesReader tr) throws IOException {
181 int flags = tr.readUINT32(); 182 int flags = tr.readUINT32();
182 if((flags & AttribFlags.SSH_FILEXFER_ATTR_SIZE) != 0) { 183
184 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_SIZE) != 0) {
183 this.size = tr.readUINT64(); 185 this.size = tr.readUINT64();
184 } 186 }
185 if((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID) != 0) { 187
188 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID) != 0) {
186 this.uid = tr.readUINT32(); 189 this.uid = tr.readUINT32();
187 this.gid = tr.readUINT32(); 190 this.gid = tr.readUINT32();
188 } 191 }
189 if((flags & AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS) != 0) { 192
193 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS) != 0) {
190 this.permissions = tr.readUINT32(); 194 this.permissions = tr.readUINT32();
191 } 195 }
192 if((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME) != 0) { 196
197 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME) != 0) {
193 this.atime = tr.readUINT32(); 198 this.atime = tr.readUINT32();
194 this.mtime = tr.readUINT32(); 199 this.mtime = tr.readUINT32();
195 200 }
196 } 201
197 if((flags & AttribFlags.SSH_FILEXFER_ATTR_EXTENDED) != 0) { 202 if ((flags & AttribFlags.SSH_FILEXFER_ATTR_EXTENDED) != 0) {
198 int count = tr.readUINT32(); 203 int count = tr.readUINT32();
204
199 // Read it anyway to detect corrupt packets 205 // Read it anyway to detect corrupt packets
200 while(count > 0) { 206 while (count > 0) {
201 tr.readByteString(); 207 tr.readByteString();
202 tr.readByteString(); 208 tr.readByteString();
203 count--; 209 count--;
204 } 210 }
205 } 211 }
213 * @return Encoded attributes 219 * @return Encoded attributes
214 */ 220 */
215 public byte[] toBytes() { 221 public byte[] toBytes() {
216 TypesWriter tw = new TypesWriter(); 222 TypesWriter tw = new TypesWriter();
217 int attrFlags = 0; 223 int attrFlags = 0;
218 if(this.size != null) { 224
225 if (this.size != null) {
219 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE; 226 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_SIZE;
220 } 227 }
221 if((this.uid != null) && (this.gid != null)) { 228
229 if ((this.uid != null) && (this.gid != null)) {
222 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID; 230 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_UIDGID;
223 } 231 }
224 if(this.permissions != null) { 232
233 if (this.permissions != null) {
225 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS; 234 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS;
226 } 235 }
227 if((this.atime != null) && (this.mtime != null)) { 236
237 if ((this.atime != null) && (this.mtime != null)) {
228 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME; 238 attrFlags = attrFlags | AttribFlags.SSH_FILEXFER_ATTR_V3_ACMODTIME;
229 } 239 }
240
230 tw.writeUINT32(attrFlags); 241 tw.writeUINT32(attrFlags);
231 if(this.size != null) { 242
243 if (this.size != null) {
232 tw.writeUINT64(this.size); 244 tw.writeUINT64(this.size);
233 } 245 }
234 246
235 if((this.uid != null) && (this.gid != null)) { 247 if ((this.uid != null) && (this.gid != null)) {
236 tw.writeUINT32(this.uid); 248 tw.writeUINT32(this.uid);
237 tw.writeUINT32(this.gid); 249 tw.writeUINT32(this.gid);
238 } 250 }
239 251
240 if(this.permissions != null) { 252 if (this.permissions != null) {
241 tw.writeUINT32(this.permissions); 253 tw.writeUINT32(this.permissions);
242 } 254 }
243 255
244 if((this.atime != null) && (this.mtime != null)) { 256 if ((this.atime != null) && (this.mtime != null)) {
245 tw.writeUINT32(this.atime); 257 tw.writeUINT32(this.atime);
246 tw.writeUINT32(this.mtime); 258 tw.writeUINT32(this.mtime);
247 } 259 }
260
248 return tw.getBytes(); 261 return tw.getBytes();
249 } 262 }
250 263
251 @Override 264 @Override
252 public String toString() { 265 public String toString() {