1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.fs; |
---|
19 | |
---|
20 | import java.io.DataInput; |
---|
21 | import java.io.DataOutput; |
---|
22 | import java.io.IOException; |
---|
23 | |
---|
24 | import org.apache.hadoop.fs.permission.FsPermission; |
---|
25 | import org.apache.hadoop.io.Text; |
---|
26 | import org.apache.hadoop.io.Writable; |
---|
27 | |
---|
28 | /** Interface that represents the client side information for a file. |
---|
29 | */ |
---|
30 | public class FileStatus implements Writable, Comparable { |
---|
31 | |
---|
32 | private Path path; |
---|
33 | private long length; |
---|
34 | private boolean isdir; |
---|
35 | private short block_replication; |
---|
36 | private long blocksize; |
---|
37 | private long modification_time; |
---|
38 | private long access_time; |
---|
39 | private FsPermission permission; |
---|
40 | private String owner; |
---|
41 | private String group; |
---|
42 | |
---|
43 | public FileStatus() { this(0, false, 0, 0, 0, 0, null, null, null, null); } |
---|
44 | |
---|
45 | //We should deprecate this soon? |
---|
46 | public FileStatus(long length, boolean isdir, int block_replication, |
---|
47 | long blocksize, long modification_time, Path path) { |
---|
48 | |
---|
49 | this(length, isdir, block_replication, blocksize, modification_time, |
---|
50 | 0, null, null, null, path); |
---|
51 | } |
---|
52 | |
---|
53 | public FileStatus(long length, boolean isdir, int block_replication, |
---|
54 | long blocksize, long modification_time, long access_time, |
---|
55 | FsPermission permission, String owner, String group, |
---|
56 | Path path) { |
---|
57 | this.length = length; |
---|
58 | this.isdir = isdir; |
---|
59 | this.block_replication = (short)block_replication; |
---|
60 | this.blocksize = blocksize; |
---|
61 | this.modification_time = modification_time; |
---|
62 | this.access_time = access_time; |
---|
63 | this.permission = (permission == null) ? |
---|
64 | FsPermission.getDefault() : permission; |
---|
65 | this.owner = (owner == null) ? "" : owner; |
---|
66 | this.group = (group == null) ? "" : group; |
---|
67 | this.path = path; |
---|
68 | } |
---|
69 | |
---|
70 | /* |
---|
71 | * @return the length of this file, in blocks |
---|
72 | */ |
---|
73 | public long getLen() { |
---|
74 | return length; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Is this a directory? |
---|
79 | * @return true if this is a directory |
---|
80 | */ |
---|
81 | public boolean isDir() { |
---|
82 | return isdir; |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Get the block size of the file. |
---|
87 | * @return the number of bytes |
---|
88 | */ |
---|
89 | public long getBlockSize() { |
---|
90 | return blocksize; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Get the replication factor of a file. |
---|
95 | * @return the replication factor of a file. |
---|
96 | */ |
---|
97 | public short getReplication() { |
---|
98 | return block_replication; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Get the modification time of the file. |
---|
103 | * @return the modification time of file in milliseconds since January 1, 1970 UTC. |
---|
104 | */ |
---|
105 | public long getModificationTime() { |
---|
106 | return modification_time; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Get the access time of the file. |
---|
111 | * @return the access time of file in milliseconds since January 1, 1970 UTC. |
---|
112 | */ |
---|
113 | public long getAccessTime() { |
---|
114 | return access_time; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Get FsPermission associated with the file. |
---|
119 | * @return permssion. If a filesystem does not have a notion of permissions |
---|
120 | * or if permissions could not be determined, then default |
---|
121 | * permissions equivalent of "rwxrwxrwx" is returned. |
---|
122 | */ |
---|
123 | public FsPermission getPermission() { |
---|
124 | return permission; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Get the owner of the file. |
---|
129 | * @return owner of the file. The string could be empty if there is no |
---|
130 | * notion of owner of a file in a filesystem or if it could not |
---|
131 | * be determined (rare). |
---|
132 | */ |
---|
133 | public String getOwner() { |
---|
134 | return owner; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Get the group associated with the file. |
---|
139 | * @return group for the file. The string could be empty if there is no |
---|
140 | * notion of group of a file in a filesystem or if it could not |
---|
141 | * be determined (rare). |
---|
142 | */ |
---|
143 | public String getGroup() { |
---|
144 | return group; |
---|
145 | } |
---|
146 | |
---|
147 | public Path getPath() { |
---|
148 | return path; |
---|
149 | } |
---|
150 | |
---|
151 | /* These are provided so that these values could be loaded lazily |
---|
152 | * by a filesystem (e.g. local file system). |
---|
153 | */ |
---|
154 | |
---|
155 | /** |
---|
156 | * Sets permission. |
---|
157 | * @param permission if permission is null, default value is set |
---|
158 | */ |
---|
159 | protected void setPermission(FsPermission permission) { |
---|
160 | this.permission = (permission == null) ? |
---|
161 | FsPermission.getDefault() : permission; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Sets owner. |
---|
166 | * @param owner if it is null, default value is set |
---|
167 | */ |
---|
168 | protected void setOwner(String owner) { |
---|
169 | this.owner = (owner == null) ? "" : owner; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * Sets group. |
---|
174 | * @param group if it is null, default value is set |
---|
175 | */ |
---|
176 | protected void setGroup(String group) { |
---|
177 | this.group = (group == null) ? "" : group; |
---|
178 | } |
---|
179 | |
---|
180 | ////////////////////////////////////////////////// |
---|
181 | // Writable |
---|
182 | ////////////////////////////////////////////////// |
---|
183 | public void write(DataOutput out) throws IOException { |
---|
184 | Text.writeString(out, getPath().toString()); |
---|
185 | out.writeLong(length); |
---|
186 | out.writeBoolean(isdir); |
---|
187 | out.writeShort(block_replication); |
---|
188 | out.writeLong(blocksize); |
---|
189 | out.writeLong(modification_time); |
---|
190 | out.writeLong(access_time); |
---|
191 | permission.write(out); |
---|
192 | Text.writeString(out, owner); |
---|
193 | Text.writeString(out, group); |
---|
194 | } |
---|
195 | |
---|
196 | public void readFields(DataInput in) throws IOException { |
---|
197 | String strPath = Text.readString(in); |
---|
198 | this.path = new Path(strPath); |
---|
199 | this.length = in.readLong(); |
---|
200 | this.isdir = in.readBoolean(); |
---|
201 | this.block_replication = in.readShort(); |
---|
202 | blocksize = in.readLong(); |
---|
203 | modification_time = in.readLong(); |
---|
204 | access_time = in.readLong(); |
---|
205 | permission.readFields(in); |
---|
206 | owner = Text.readString(in); |
---|
207 | group = Text.readString(in); |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | * Compare this object to another object |
---|
212 | * |
---|
213 | * @param o the object to be compared. |
---|
214 | * @return a negative integer, zero, or a positive integer as this object |
---|
215 | * is less than, equal to, or greater than the specified object. |
---|
216 | * |
---|
217 | * @throws ClassCastException if the specified object's is not of |
---|
218 | * type FileStatus |
---|
219 | */ |
---|
220 | public int compareTo(Object o) { |
---|
221 | FileStatus other = (FileStatus)o; |
---|
222 | return this.getPath().compareTo(other.getPath()); |
---|
223 | } |
---|
224 | |
---|
225 | /** Compare if this object is equal to another object |
---|
226 | * @param o the object to be compared. |
---|
227 | * @return true if two file status has the same path name; false if not. |
---|
228 | */ |
---|
229 | public boolean equals(Object o) { |
---|
230 | if (o == null) { |
---|
231 | return false; |
---|
232 | } |
---|
233 | if (this == o) { |
---|
234 | return true; |
---|
235 | } |
---|
236 | if (!(o instanceof FileStatus)) { |
---|
237 | return false; |
---|
238 | } |
---|
239 | FileStatus other = (FileStatus)o; |
---|
240 | return this.getPath().equals(other.getPath()); |
---|
241 | } |
---|
242 | |
---|
243 | /** |
---|
244 | * Returns a hash code value for the object, which is defined as |
---|
245 | * the hash code of the path name. |
---|
246 | * |
---|
247 | * @return a hash code value for the path name. |
---|
248 | */ |
---|
249 | public int hashCode() { |
---|
250 | return getPath().hashCode(); |
---|
251 | } |
---|
252 | } |
---|