blob: 9a63feeb555dd536004ac2614351dd2bf9f8ea9b [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.unix;
15
16/**
17 * <p>Equivalent to UNIX's "struct stat", a FileStatus instance contains
18 * various bits of metadata about a directory entry.
19 *
20 * <p>The Java SDK provides access to some but not all of the information
21 * available via the stat(2) and lstat(2) syscalls, but often requires that
22 * multiple calls be made to obtain it. By reifying stat buffers as Java
23 * objects and providing a wrapper around the stat/lstat calls, we give client
24 * applications access to the richer file metadata and enable a reduction in
25 * the number of system calls, which is critical for high-performance tools.
26 *
27 * <p>This class is optimized for memory usage. Operations that are not yet
28 * required for any client are intentionally unimplemented to save space.
29 * Currently, we only support these fields: st_mode, st_size, st_atime,
30 * st_atimensec, st_mtime, st_mtimensec, st_ctime, st_ctimensec, st_dev, st_ino.
31 * Methods that require other fields throw UnsupportedOperationException.
32 */
33public class FileStatus {
34
35 private final int st_mode;
36 private final int st_atime; // (unsigned)
37 private final int st_atimensec; // (unsigned)
38 private final int st_mtime; // (unsigned)
39 private final int st_mtimensec; // (unsigned)
40 private final int st_ctime; // (unsigned)
41 private final int st_ctimensec; // (unsigned)
42 private final long st_size;
43 private final int st_dev;
44 private final long st_ino;
45
46 /**
47 * Constructs a FileStatus instance. (Called only from JNI code.)
48 */
49 protected FileStatus(int st_mode, int st_atime, int st_atimensec, int st_mtime, int st_mtimensec,
50 int st_ctime, int st_ctimensec, long st_size, int st_dev, long st_ino) {
51 this.st_mode = st_mode;
52 this.st_atime = st_atime;
53 this.st_atimensec = st_atimensec;
54 this.st_mtime = st_mtime;
55 this.st_mtimensec = st_mtimensec;
56 this.st_ctime = st_ctime;
57 this.st_ctimensec = st_ctimensec;
58 this.st_size = st_size;
59 this.st_dev = st_dev;
60 this.st_ino = st_ino;
61 }
62
63 /**
64 * Returns the device number of this inode.
65 */
66 public int getDeviceNumber() {
67 return st_dev;
68 }
69
70 /**
71 * Returns the number of this inode. Inode numbers are (usually) unique for
72 * a given device.
73 */
74 public long getInodeNumber() {
75 return st_ino;
76 }
77
78 /**
79 * Returns true iff this file is a regular file.
80 */
81 public boolean isRegularFile() {
82 return (st_mode & S_IFMT) == S_IFREG;
83 }
84
85 /**
86 * Returns true iff this file is a directory.
87 */
88 public boolean isDirectory() {
89 return (st_mode & S_IFMT) == S_IFDIR;
90 }
91
92 /**
93 * Returns true iff this file is a symbolic link.
94 */
95 public boolean isSymbolicLink() {
96 return (st_mode & S_IFMT) == S_IFLNK;
97 }
98
99 /**
100 * Returns true iff this file is a character device.
101 */
102 public boolean isCharacterDevice() {
103 return (st_mode & S_IFMT) == S_IFCHR;
104 }
105
106 /**
107 * Returns true iff this file is a block device.
108 */
109 public boolean isBlockDevice() {
110 return (st_mode & S_IFMT) == S_IFBLK;
111 }
112
113 /**
114 * Returns true iff this file is a FIFO.
115 */
116 public boolean isFIFO() {
117 return (st_mode & S_IFMT) == S_IFIFO;
118 }
119
120 /**
121 * Returns true iff this file is a UNIX-domain socket.
122 */
123 public boolean isSocket() {
124 return (st_mode & S_IFMT) == S_IFSOCK;
125 }
126
127 /**
128 * Returns true iff this file has its "set UID" bit set.
129 */
130 public boolean isSetUserId() {
131 return (st_mode & S_ISUID) != 0;
132 }
133
134 /**
135 * Returns true iff this file has its "set GID" bit set.
136 */
137 public boolean isSetGroupId() {
138 return (st_mode & S_ISGID) != 0;
139 }
140
141 /**
142 * Returns true iff this file has its "sticky" bit set. See UNIX manuals for
143 * explanation.
144 */
145 public boolean isSticky() {
146 return (st_mode & S_ISVTX) != 0;
147 }
148
149 /**
150 * Returns the user/group/other permissions part of the mode bits (i.e.
151 * st_mode masked with 0777), interpreted according to longstanding UNIX
152 * tradition.
153 */
154 public int getPermissions() {
155 return st_mode & S_IRWXA;
156 }
157
158 /**
159 * Returns the total size, in bytes, of this file.
160 */
161 public long getSize() {
162 return st_size;
163 }
164
165 /**
166 * Returns the last access time of this file (seconds since UNIX epoch).
167 */
168 public long getLastAccessTime() {
169 return unsignedIntToLong(st_atime);
170 }
171
172 /**
173 * Returns the fractional part of the last access time of this file (nanoseconds).
174 */
175 public long getFractionalLastAccessTime() {
176 return unsignedIntToLong(st_atimensec);
177 }
178
179 /**
180 * Returns the last modified time of this file (seconds since UNIX epoch).
181 */
182 public long getLastModifiedTime() {
183 return unsignedIntToLong(st_mtime);
184 }
185
186 /**
187 * Returns the fractional part of the last modified time of this file (nanoseconds).
188 */
189 public long getFractionalLastModifiedTime() {
190 return unsignedIntToLong(st_mtimensec);
191 }
192
193 /**
194 * Returns the last change time of this file (seconds since UNIX epoch).
195 */
196 public long getLastChangeTime() {
197 return unsignedIntToLong(st_ctime);
198 }
199
200 /**
201 * Returns the fractional part of the last change time of this file (nanoseconds).
202 */
203 public long getFractionalLastChangeTime() {
204 return unsignedIntToLong(st_ctimensec);
205 }
206
207 ////////////////////////////////////////////////////////////////////////
208
209 @Override
210 public String toString() {
211 return String.format("FileStatus(mode=0%06o,size=%d,mtime=%d)",
212 st_mode, st_size, st_mtime);
213 }
214
215 @Override
216 public int hashCode() {
217 return st_mode;
218 }
219
220 ////////////////////////////////////////////////////////////////////////
221 // Platform-specific details. These fields are public so that they can
222 // be used from other packages. See POSIX and/or Linux manuals for details.
223 //
224 // These need to be kept in sync with the native code and system call
225 // interface. (The unit tests ensure that.) Of course, this decoding could
226 // be done in the JNI code to ensure maximum portability, but (a) we don't
227 // expect we'll need that any time soon, and (b) that would require eager
228 // rather than on-demand bitmunging of all attributes. In any case, it's not
229 // part of the interface so it can be easily changed later if necessary.
230
231 public static final int S_IFMT = 0170000; // mask: filetype bitfields
232 public static final int S_IFSOCK = 0140000; // socket
233 public static final int S_IFLNK = 0120000; // symbolic link
234 public static final int S_IFREG = 0100000; // regular file
235 public static final int S_IFBLK = 0060000; // block device
236 public static final int S_IFDIR = 0040000; // directory
237 public static final int S_IFCHR = 0020000; // character device
238 public static final int S_IFIFO = 0010000; // fifo
239 public static final int S_ISUID = 0004000; // set UID bit
240 public static final int S_ISGID = 0002000; // set GID bit (see below)
241 public static final int S_ISVTX = 0001000; // sticky bit (see below)
242 public static final int S_IRWXA = 00777; // mask: all permissions
243 public static final int S_IRWXU = 00700; // mask: file owner permissions
244 public static final int S_IRUSR = 00400; // owner has read permission
245 public static final int S_IWUSR = 00200; // owner has write permission
246 public static final int S_IXUSR = 00100; // owner has execute permission
247 public static final int S_IRWXG = 00070; // mask: group permissions
248 public static final int S_IRGRP = 00040; // group has read permission
249 public static final int S_IWGRP = 00020; // group has write permission
250 public static final int S_IXGRP = 00010; // group has execute permission
251 public static final int S_IRWXO = 00007; // mask: other permissions
252 public static final int S_IROTH = 00004; // others have read permission
253 public static final int S_IWOTH = 00002; // others have write permisson
254 public static final int S_IXOTH = 00001; // others have execute permission
255
256 public static final int S_IEXEC = 00111; // owner, group, world execute
257
258 static long unsignedIntToLong(int i) {
259 return (i & 0x7FFFFFFF) - (long) (i & 0x80000000);
260 }
261
Nathan Harmatad8b6ff22015-10-20 21:54:34 +0000262 public static boolean isFile(int rawType) {
263 int type = rawType & S_IFMT;
264 return type == S_IFREG || isSpecialFile(rawType);
265 }
266
267 public static boolean isSpecialFile(int rawType) {
268 int type = rawType & S_IFMT;
269 return type == S_IFSOCK || type == S_IFBLK || type == S_IFCHR || type == S_IFIFO;
270 }
271
272 public static boolean isDirectory(int rawType) {
273 int type = rawType & S_IFMT;
274 return type == S_IFDIR;
275 }
276
277 public static boolean isSymbolicLink(int rawType) {
278 int type = rawType & S_IFMT;
279 return type == S_IFLNK;
280 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100281}