1/*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5package ch.ethz.ssh2;
6
7/**
8 * A <code>SFTPv3FileAttributes</code> object represents detail information
9 * about a file on the server. Not all fields may/must be present.
10 *
11 * @author Christian Plattner, plattner@inf.ethz.ch
12 * @version $Id: SFTPv3FileAttributes.java 42 2011-06-02 15:34:20Z dkocher@sudo.ch $
13 */
14
15public class SFTPv3FileAttributes
16{
17	/**
18	 * The SIZE attribute. <code>NULL</code> if not present.
19	 */
20	public Long size = null;
21
22	/**
23	 * The UID attribute. <code>NULL</code> if not present.
24	 */
25	public Integer uid = null;
26
27	/**
28	 * The GID attribute. <code>NULL</code> if not present.
29	 */
30	public Integer gid = null;
31
32	/**
33	 * The POSIX permissions. <code>NULL</code> if not present.
34	 * <p>
35	 * Here is a list:
36	 * <p>
37	 * <pre>Note: these numbers are all OCTAL.
38	 *
39	 *  S_IFMT     0170000   bitmask for the file type bitfields
40	 *  S_IFSOCK   0140000   socket
41	 *  S_IFLNK    0120000   symbolic link
42	 *  S_IFREG    0100000   regular file
43	 *  S_IFBLK    0060000   block device
44	 *  S_IFDIR    0040000   directory
45	 *  S_IFCHR    0020000   character device
46	 *  S_IFIFO    0010000   fifo
47	 *  S_ISUID    0004000   set UID bit
48	 *  S_ISGID    0002000   set GID bit
49	 *  S_ISVTX    0001000   sticky bit
50	 *
51	 *  S_IRWXU    00700     mask for file owner permissions
52	 *  S_IRUSR    00400     owner has read permission
53	 *  S_IWUSR    00200     owner has write permission
54	 *  S_IXUSR    00100     owner has execute permission
55	 *  S_IRWXG    00070     mask for group permissions
56	 *  S_IRGRP    00040     group has read permission
57	 *  S_IWGRP    00020     group has write permission
58	 *  S_IXGRP    00010     group has execute permission
59	 *  S_IRWXO    00007     mask for permissions for others (not in group)
60	 *  S_IROTH    00004     others have read permission
61	 *  S_IWOTH    00002     others have write permisson
62	 *  S_IXOTH    00001     others have execute permission
63	 * </pre>
64	 */
65	public Integer permissions = null;
66
67	/**
68	 * The ATIME attribute. Represented as seconds from Jan 1, 1970 in UTC.
69	 * <code>NULL</code> if not present.
70	 */
71	public Integer atime = null;
72
73	/**
74	 * The MTIME attribute. Represented as seconds from Jan 1, 1970 in UTC.
75	 * <code>NULL</code> if not present.
76	 */
77	public Integer mtime = null;
78
79	/**
80	 * Checks if this entry is a directory.
81	 *
82	 * @return Returns true if permissions are available and they indicate
83	 *         that this entry represents a directory.
84	 */
85	public boolean isDirectory()
86	{
87		if (permissions == null)
88			return false;
89
90		return ((permissions.intValue() & 0040000) == 0040000);
91	}
92
93	/**
94	 * Checks if this entry is a regular file.
95	 *
96	 * @return Returns true if permissions are available and they indicate
97	 *         that this entry represents a regular file.
98	 */
99	public boolean isRegularFile()
100	{
101		if (permissions == null)
102			return false;
103
104		return ((permissions.intValue() & 0100000) == 0100000);
105	}
106
107	/**
108	 * Checks if this entry is a a symlink.
109	 *
110	 * @return Returns true if permissions are available and they indicate
111	 *         that this entry represents a symlink.
112	 */
113	public boolean isSymlink()
114	{
115		if (permissions == null)
116			return false;
117
118		return ((permissions.intValue() & 0120000) == 0120000);
119	}
120
121	/**
122	 * Turn the POSIX permissions into a 7 digit octal representation.
123	 * Note: the returned value is first masked with <code>0177777</code>.
124	 *
125	 * @return <code>NULL</code> if permissions are not available.
126	 */
127	public String getOctalPermissions()
128	{
129		if (permissions == null)
130			return null;
131
132		String res = Integer.toString(permissions.intValue() & 0177777, 8);
133
134		StringBuilder sb = new StringBuilder();
135
136		int leadingZeros = 7 - res.length();
137
138		while (leadingZeros > 0)
139		{
140			sb.append('0');
141			leadingZeros--;
142		}
143
144		sb.append(res);
145
146		return sb.toString();
147	}
148}