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 */ 018package org.apache.hadoop.fs.permission; 019 020import org.apache.hadoop.classification.InterfaceAudience; 021import org.apache.hadoop.classification.InterfaceStability; 022 023/** 024 * File system actions, e.g. read, write, etc. 025 */ 026@InterfaceAudience.Public 027@InterfaceStability.Stable 028public enum FsAction { 029 // POSIX style 030 NONE("---"), 031 EXECUTE("--x"), 032 WRITE("-w-"), 033 WRITE_EXECUTE("-wx"), 034 READ("r--"), 035 READ_EXECUTE("r-x"), 036 READ_WRITE("rw-"), 037 ALL("rwx"); 038 039 /** Retain reference to value array. */ 040 private final static FsAction[] vals = values(); 041 042 /** Symbolic representation */ 043 public final String SYMBOL; 044 045 private FsAction(String s) { 046 SYMBOL = s; 047 } 048 049 /** 050 * Return true if this action implies that action. 051 * @param that 052 */ 053 public boolean implies(FsAction that) { 054 if (that != null) { 055 return (ordinal() & that.ordinal()) == that.ordinal(); 056 } 057 return false; 058 } 059 060 /** AND operation. */ 061 public FsAction and(FsAction that) { 062 return vals[ordinal() & that.ordinal()]; 063 } 064 /** OR operation. */ 065 public FsAction or(FsAction that) { 066 return vals[ordinal() | that.ordinal()]; 067 } 068 /** NOT operation. */ 069 public FsAction not() { 070 return vals[7 - ordinal()]; 071 } 072 073 /** 074 * Get the FsAction enum for String representation of permissions 075 * 076 * @param permission 077 * 3-character string representation of permission. ex: rwx 078 * @return Returns FsAction enum if the corresponding FsAction exists for permission. 079 * Otherwise returns null 080 */ 081 public static FsAction getFsAction(String permission) { 082 for (FsAction fsAction : vals) { 083 if (fsAction.SYMBOL.equals(permission)) { 084 return fsAction; 085 } 086 } 087 return null; 088 } 089}