001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.hadoop.fs.permission;
019
020 import java.io.DataInput;
021 import java.io.DataOutput;
022 import java.io.IOException;
023
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026 import org.apache.hadoop.classification.InterfaceAudience;
027 import org.apache.hadoop.classification.InterfaceStability;
028 import org.apache.hadoop.conf.Configuration;
029 import org.apache.hadoop.fs.CommonConfigurationKeys;
030 import org.apache.hadoop.io.Writable;
031 import org.apache.hadoop.io.WritableFactories;
032 import org.apache.hadoop.io.WritableFactory;
033
034 /**
035 * A class for file/directory permissions.
036 */
037 @InterfaceAudience.Public
038 @InterfaceStability.Stable
039 public class FsPermission implements Writable {
040 private static final Log LOG = LogFactory.getLog(FsPermission.class);
041
042 static final WritableFactory FACTORY = new WritableFactory() {
043 @Override
044 public Writable newInstance() { return new FsPermission(); }
045 };
046 static { // register a ctor
047 WritableFactories.setFactory(FsPermission.class, FACTORY);
048 WritableFactories.setFactory(ImmutableFsPermission.class, FACTORY);
049 }
050
051 /** Create an immutable {@link FsPermission} object. */
052 public static FsPermission createImmutable(short permission) {
053 return new ImmutableFsPermission(permission);
054 }
055
056 //POSIX permission style
057 private FsAction useraction = null;
058 private FsAction groupaction = null;
059 private FsAction otheraction = null;
060 private boolean stickyBit = false;
061
062 private FsPermission() {}
063
064 /**
065 * Construct by the given {@link FsAction}.
066 * @param u user action
067 * @param g group action
068 * @param o other action
069 */
070 public FsPermission(FsAction u, FsAction g, FsAction o) {
071 this(u, g, o, false);
072 }
073
074 public FsPermission(FsAction u, FsAction g, FsAction o, boolean sb) {
075 set(u, g, o, sb);
076 }
077
078 /**
079 * Construct by the given mode.
080 * @param mode
081 * @see #toShort()
082 */
083 public FsPermission(short mode) { fromShort(mode); }
084
085 /**
086 * Copy constructor
087 *
088 * @param other other permission
089 */
090 public FsPermission(FsPermission other) {
091 this.useraction = other.useraction;
092 this.groupaction = other.groupaction;
093 this.otheraction = other.otheraction;
094 this.stickyBit = other.stickyBit;
095 }
096
097 /**
098 * Construct by given mode, either in octal or symbolic format.
099 * @param mode mode as a string, either in octal or symbolic format
100 * @throws IllegalArgumentException if <code>mode</code> is invalid
101 */
102 public FsPermission(String mode) {
103 this(new UmaskParser(mode).getUMask());
104 }
105
106 /** Return user {@link FsAction}. */
107 public FsAction getUserAction() {return useraction;}
108
109 /** Return group {@link FsAction}. */
110 public FsAction getGroupAction() {return groupaction;}
111
112 /** Return other {@link FsAction}. */
113 public FsAction getOtherAction() {return otheraction;}
114
115 private void set(FsAction u, FsAction g, FsAction o, boolean sb) {
116 useraction = u;
117 groupaction = g;
118 otheraction = o;
119 stickyBit = sb;
120 }
121
122 public void fromShort(short n) {
123 FsAction[] v = FSACTION_VALUES;
124 set(v[(n >>> 6) & 7], v[(n >>> 3) & 7], v[n & 7], (((n >>> 9) & 1) == 1) );
125 }
126
127 @Override
128 public void write(DataOutput out) throws IOException {
129 out.writeShort(toShort());
130 }
131
132 @Override
133 public void readFields(DataInput in) throws IOException {
134 fromShort(in.readShort());
135 }
136
137 /**
138 * Create and initialize a {@link FsPermission} from {@link DataInput}.
139 */
140 public static FsPermission read(DataInput in) throws IOException {
141 FsPermission p = new FsPermission();
142 p.readFields(in);
143 return p;
144 }
145
146 /**
147 * Encode the object to a short.
148 */
149 public short toShort() {
150 int s = (stickyBit ? 1 << 9 : 0) |
151 (useraction.ordinal() << 6) |
152 (groupaction.ordinal() << 3) |
153 otheraction.ordinal();
154
155 return (short)s;
156 }
157
158 @Override
159 public boolean equals(Object obj) {
160 if (obj instanceof FsPermission) {
161 FsPermission that = (FsPermission)obj;
162 return this.useraction == that.useraction
163 && this.groupaction == that.groupaction
164 && this.otheraction == that.otheraction
165 && this.stickyBit == that.stickyBit;
166 }
167 return false;
168 }
169
170 @Override
171 public int hashCode() {return toShort();}
172
173 @Override
174 public String toString() {
175 String str = useraction.SYMBOL + groupaction.SYMBOL + otheraction.SYMBOL;
176 if(stickyBit) {
177 StringBuilder str2 = new StringBuilder(str);
178 str2.replace(str2.length() - 1, str2.length(),
179 otheraction.implies(FsAction.EXECUTE) ? "t" : "T");
180 str = str2.toString();
181 }
182
183 return str;
184 }
185
186 /**
187 * Apply a umask to this permission and return a new one.
188 *
189 * The umask is used by create, mkdir, and other Hadoop filesystem operations.
190 * The mode argument for these operations is modified by removing the bits
191 * which are set in the umask. Thus, the umask limits the permissions which
192 * newly created files and directories get.
193 *
194 * @param umask The umask to use
195 *
196 * @return The effective permission
197 */
198 public FsPermission applyUMask(FsPermission umask) {
199 return new FsPermission(useraction.and(umask.useraction.not()),
200 groupaction.and(umask.groupaction.not()),
201 otheraction.and(umask.otheraction.not()));
202 }
203
204 /** umask property label deprecated key and code in getUMask method
205 * to accommodate it may be removed in version .23 */
206 public static final String DEPRECATED_UMASK_LABEL = "dfs.umask";
207 public static final String UMASK_LABEL =
208 CommonConfigurationKeys.FS_PERMISSIONS_UMASK_KEY;
209 public static final int DEFAULT_UMASK =
210 CommonConfigurationKeys.FS_PERMISSIONS_UMASK_DEFAULT;
211
212 private static final FsAction[] FSACTION_VALUES = FsAction.values();
213
214 /**
215 * Get the user file creation mask (umask)
216 *
217 * {@code UMASK_LABEL} config param has umask value that is either symbolic
218 * or octal.
219 *
220 * Symbolic umask is applied relative to file mode creation mask;
221 * the permission op characters '+' clears the corresponding bit in the mask,
222 * '-' sets bits in the mask.
223 *
224 * Octal umask, the specified bits are set in the file mode creation mask.
225 *
226 * {@code DEPRECATED_UMASK_LABEL} config param has umask value set to decimal.
227 */
228 public static FsPermission getUMask(Configuration conf) {
229 int umask = DEFAULT_UMASK;
230
231 // To ensure backward compatibility first use the deprecated key.
232 // If the deprecated key is not present then check for the new key
233 if(conf != null) {
234 String confUmask = conf.get(UMASK_LABEL);
235 int oldUmask = conf.getInt(DEPRECATED_UMASK_LABEL, Integer.MIN_VALUE);
236 try {
237 if(confUmask != null) {
238 umask = new UmaskParser(confUmask).getUMask();
239 }
240 } catch(IllegalArgumentException iae) {
241 // Provide more explanation for user-facing message
242 String type = iae instanceof NumberFormatException ? "decimal"
243 : "octal or symbolic";
244 String error = "Unable to parse configuration " + UMASK_LABEL
245 + " with value " + confUmask + " as " + type + " umask.";
246 LOG.warn(error);
247
248 // If oldUmask is not set, then throw the exception
249 if (oldUmask == Integer.MIN_VALUE) {
250 throw new IllegalArgumentException(error);
251 }
252 }
253
254 if(oldUmask != Integer.MIN_VALUE) { // Property was set with old key
255 if (umask != oldUmask) {
256 LOG.warn(DEPRECATED_UMASK_LABEL
257 + " configuration key is deprecated. " + "Convert to "
258 + UMASK_LABEL + ", using octal or symbolic umask "
259 + "specifications.");
260 // Old and new umask values do not match - Use old umask
261 umask = oldUmask;
262 }
263 }
264 }
265
266 return new FsPermission((short)umask);
267 }
268
269 public boolean getStickyBit() {
270 return stickyBit;
271 }
272
273 /** Set the user file creation mask (umask) */
274 public static void setUMask(Configuration conf, FsPermission umask) {
275 conf.set(UMASK_LABEL, String.format("%1$03o", umask.toShort()));
276 conf.setInt(DEPRECATED_UMASK_LABEL, umask.toShort());
277 }
278
279 /**
280 * Get the default permission for directory and symlink.
281 * In previous versions, this default permission was also used to
282 * create files, so files created end up with ugo+x permission.
283 * See HADOOP-9155 for detail.
284 * Two new methods are added to solve this, please use
285 * {@link FsPermission#getDirDefault()} for directory, and use
286 * {@link FsPermission#getFileDefault()} for file.
287 * This method is kept for compatibility.
288 */
289 public static FsPermission getDefault() {
290 return new FsPermission((short)00777);
291 }
292
293 /**
294 * Get the default permission for directory.
295 */
296 public static FsPermission getDirDefault() {
297 return new FsPermission((short)00777);
298 }
299
300 /**
301 * Get the default permission for file.
302 */
303 public static FsPermission getFileDefault() {
304 return new FsPermission((short)00666);
305 }
306
307 /**
308 * Get the default permission for cache pools.
309 */
310 public static FsPermission getCachePoolDefault() {
311 return new FsPermission((short)00755);
312 }
313
314 /**
315 * Create a FsPermission from a Unix symbolic permission string
316 * @param unixSymbolicPermission e.g. "-rw-rw-rw-"
317 */
318 public static FsPermission valueOf(String unixSymbolicPermission) {
319 if (unixSymbolicPermission == null) {
320 return null;
321 }
322 else if (unixSymbolicPermission.length() != 10) {
323 throw new IllegalArgumentException("length != 10(unixSymbolicPermission="
324 + unixSymbolicPermission + ")");
325 }
326
327 int n = 0;
328 for(int i = 1; i < unixSymbolicPermission.length(); i++) {
329 n = n << 1;
330 char c = unixSymbolicPermission.charAt(i);
331 n += (c == '-' || c == 'T' || c == 'S') ? 0: 1;
332 }
333
334 // Add sticky bit value if set
335 if(unixSymbolicPermission.charAt(9) == 't' ||
336 unixSymbolicPermission.charAt(9) == 'T')
337 n += 01000;
338
339 return new FsPermission((short)n);
340 }
341
342 private static class ImmutableFsPermission extends FsPermission {
343 public ImmutableFsPermission(short permission) {
344 super(permission);
345 }
346 @Override
347 public FsPermission applyUMask(FsPermission umask) {
348 throw new UnsupportedOperationException();
349 }
350 @Override
351 public void readFields(DataInput in) throws IOException {
352 throw new UnsupportedOperationException();
353 }
354 }
355 }