|
@@ -0,0 +1,176 @@
|
|
|
+/**
|
|
|
+ * Licensed to the Apache Software Foundation (ASF) under one
|
|
|
+ * or more contributor license agreements. See the NOTICE file
|
|
|
+ * distributed with this work for additional information
|
|
|
+ * regarding copyright ownership. The ASF licenses this file
|
|
|
+ * to you under the Apache License, Version 2.0 (the
|
|
|
+ * "License"); you may not use this file except in compliance
|
|
|
+ * with the License. You may obtain a copy of the License at
|
|
|
+ *
|
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+ *
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+ * See the License for the specific language governing permissions and
|
|
|
+ * limitations under the License.
|
|
|
+ */
|
|
|
+package org.apache.hadoop.fs.permission;
|
|
|
+
|
|
|
+import org.apache.hadoop.conf.Configuration;
|
|
|
+import org.apache.hadoop.io.*;
|
|
|
+
|
|
|
+import java.io.DataInput;
|
|
|
+import java.io.DataOutput;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * A class for file/directory permissions.
|
|
|
+ */
|
|
|
+public class FsPermission implements Writable {
|
|
|
+ static final WritableFactory FACTORY = new WritableFactory() {
|
|
|
+ public Writable newInstance() { return new FsPermission(); }
|
|
|
+ };
|
|
|
+ static { // register a ctor
|
|
|
+ WritableFactories.setFactory(FsPermission.class, FACTORY);
|
|
|
+ }
|
|
|
+
|
|
|
+ //POSIX permission style
|
|
|
+ private FsAction useraction = null;
|
|
|
+ private FsAction groupaction = null;
|
|
|
+ private FsAction otheraction = null;
|
|
|
+
|
|
|
+ private FsPermission() {}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Construct by the given {@link FsAction}.
|
|
|
+ * @param u user action
|
|
|
+ * @param g group action
|
|
|
+ * @param o other action
|
|
|
+ */
|
|
|
+ public FsPermission(FsAction u, FsAction g, FsAction o) {set(u, g, o);}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Construct by the given mode.
|
|
|
+ * @param mode
|
|
|
+ * @see #toShort()
|
|
|
+ */
|
|
|
+ public FsPermission(short mode) { fromShort(mode); }
|
|
|
+
|
|
|
+ /** Return user {@link FsAction}. */
|
|
|
+ public FsAction getUserAction() {return useraction;}
|
|
|
+
|
|
|
+ /** Return group {@link FsAction}. */
|
|
|
+ public FsAction getGroupAction() {return groupaction;}
|
|
|
+
|
|
|
+ /** Return other {@link FsAction}. */
|
|
|
+ public FsAction getOtherAction() {return otheraction;}
|
|
|
+
|
|
|
+ private void set(FsAction u, FsAction g, FsAction o) {
|
|
|
+ useraction = u;
|
|
|
+ groupaction = g;
|
|
|
+ otheraction = o;
|
|
|
+ }
|
|
|
+ private void fromShort(short n) {
|
|
|
+ FsAction[] v = FsAction.values();
|
|
|
+ set(v[(n >>> 6) & 7], v[(n >>> 3) & 7], v[n & 7]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** {@inheritDoc} */
|
|
|
+ public void write(DataOutput out) throws IOException {
|
|
|
+ out.writeShort(toShort());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** {@inheritDoc} */
|
|
|
+ public void readFields(DataInput in) throws IOException {
|
|
|
+ fromShort(in.readShort());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create and initialize a {@link FsPermission} from {@link DataInput}.
|
|
|
+ */
|
|
|
+ public static FsPermission read(DataInput in) throws IOException {
|
|
|
+ FsPermission p = new FsPermission();
|
|
|
+ p.readFields(in);
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Encode the object to a short.
|
|
|
+ */
|
|
|
+ public short toShort() {
|
|
|
+ int s = (useraction.INDEX<<6) | (groupaction.INDEX<<3) | otheraction.INDEX;
|
|
|
+ return (short)s;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** {@inheritDoc} */
|
|
|
+ public boolean equals(Object obj) {
|
|
|
+ if (obj instanceof FsPermission) {
|
|
|
+ FsPermission that = (FsPermission)obj;
|
|
|
+ return this.useraction == that.useraction
|
|
|
+ && this.groupaction == that.groupaction
|
|
|
+ && this.otheraction == that.otheraction;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** {@inheritDoc} */
|
|
|
+ public int hashCode() {return toShort();}
|
|
|
+
|
|
|
+ /** {@inheritDoc} */
|
|
|
+ public String toString() {
|
|
|
+ return useraction.SYMBOL + groupaction.SYMBOL + otheraction.SYMBOL;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** Apply a umask to this permission and return a new one */
|
|
|
+ public FsPermission applyUMask(FsPermission umask) {
|
|
|
+ return new FsPermission(useraction.and(umask.useraction.not()),
|
|
|
+ groupaction.and(umask.groupaction.not()),
|
|
|
+ otheraction.and(umask.otheraction.not()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** umask property label */
|
|
|
+ public static final String UMASK_LABEL = "hadoop.dfs.umask";
|
|
|
+ public static final int DEFAULT_UMASK = 0022;
|
|
|
+
|
|
|
+ /** Get the user file creation mask (umask) */
|
|
|
+ public static FsPermission getUMask(Configuration conf) {
|
|
|
+ int umask = DEFAULT_UMASK;
|
|
|
+ if (conf != null) {
|
|
|
+ umask = conf.getInt(UMASK_LABEL, DEFAULT_UMASK);
|
|
|
+ }
|
|
|
+ return new FsPermission((short)umask);
|
|
|
+ }
|
|
|
+ /** Set the user file creation mask (umask) */
|
|
|
+ public static void setUMask(Configuration conf, FsPermission umask) {
|
|
|
+ conf.setInt(UMASK_LABEL, umask.toShort());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final FsPermission NONE = new FsPermission((short)0);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get the default permission from conf.
|
|
|
+ * @param conf
|
|
|
+ */
|
|
|
+ public static FsPermission getDefault(Configuration conf) {
|
|
|
+ return NONE.applyUMask(getUMask(conf));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create a FsPermission from a Unix symbolic permission string
|
|
|
+ * @param unixSymbolicPermission e.g. "-rw-rw-rw-"
|
|
|
+ */
|
|
|
+ public static FsPermission valueOf(String unixSymbolicPermission) {
|
|
|
+ if (unixSymbolicPermission.length() != 10) {
|
|
|
+ throw new IllegalArgumentException("length != 10(unixSymbolicPermission="
|
|
|
+ + unixSymbolicPermission + ")");
|
|
|
+ }
|
|
|
+ int n = 0;
|
|
|
+ for(int i = 1; i < unixSymbolicPermission.length(); i++) {
|
|
|
+ n = n << 1;
|
|
|
+ char c = unixSymbolicPermission.charAt(i);
|
|
|
+ n += c == '-' || c == 'T'? 0: 1;
|
|
|
+ }
|
|
|
+ return new FsPermission((short)n);
|
|
|
+ }
|
|
|
+}
|