| 1 | /******************************************************************** |
| 2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Author: Zhanwei Wang |
| 6 | ********************************************************************/ |
| 7 | /******************************************************************** |
| 8 | * 2014 - |
| 9 | * open source under Apache License Version 2.0 |
| 10 | ********************************************************************/ |
| 11 | /** |
| 12 | * Licensed to the Apache Software Foundation (ASF) under one |
| 13 | * or more contributor license agreements. See the NOTICE file |
| 14 | * distributed with this work for additional information |
| 15 | * regarding copyright ownership. The ASF licenses this file |
| 16 | * to you under the Apache License, Version 2.0 (the |
| 17 | * "License"); you may not use this file except in compliance |
| 18 | * with the License. You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | */ |
| 28 | #ifndef _HDFS_LIBHDFS3_CLIENT_PERMISSION_H_ |
| 29 | #define _HDFS_LIBHDFS3_CLIENT_PERMISSION_H_ |
| 30 | |
| 31 | #include <string> |
| 32 | |
| 33 | namespace Hdfs { |
| 34 | |
| 35 | /** |
| 36 | * Action is used to describe a action the user is permitted to apply on a file. |
| 37 | */ |
| 38 | enum Action { |
| 39 | NONE, //("---"), |
| 40 | EXECUTE, //("--x"), |
| 41 | WRITE, //("-w-"), |
| 42 | WRITE_EXECUTE, //("-wx"), |
| 43 | READ, //("r--"), |
| 44 | READ_EXECUTE, //("r-x"), |
| 45 | READ_WRITE, //("rw-"), |
| 46 | ALL //("rwx"); |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * To test Action a if implies Action b |
| 51 | * @param a Action to be tested. |
| 52 | * @param b Action target. |
| 53 | * @return return true if a implies b. |
| 54 | */ |
| 55 | static inline bool implies(const Action & a, const Action & b) { |
| 56 | return (a & b) == b; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * To construct a new Action using a and b |
| 61 | * @param a Action to be used. |
| 62 | * @param b Action to be used. |
| 63 | * @return return a new Action. |
| 64 | */ |
| 65 | static inline Action operator &(const Action & a, const Action & b) { |
| 66 | return (Action)(((unsigned int) a) & (unsigned int) b); |
| 67 | } |
| 68 | /** |
| 69 | * To construct a new Action using a or b |
| 70 | * @param a Action to be used. |
| 71 | * @param b Action to be used. |
| 72 | * @return return a new Action. |
| 73 | */ |
| 74 | static inline Action operator |(const Action & a, const Action & b) { |
| 75 | return (Action)(((unsigned int) a) | (unsigned int) b); |
| 76 | } |
| 77 | /** |
| 78 | * To construct a new Action of complementary of a given Action |
| 79 | * @param a Action to be used. |
| 80 | * @return return a new Action |
| 81 | */ |
| 82 | static inline Action operator ~(const Action & a) { |
| 83 | return (Action)(7 - (unsigned int) a); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * To convert a Action to a readable string. |
| 88 | * @param a the Action to be convert. |
| 89 | * @return a readable string |
| 90 | */ |
| 91 | static inline std::string toString(const Action & a) { |
| 92 | switch (a) { |
| 93 | case NONE: |
| 94 | return "---" ; |
| 95 | |
| 96 | case EXECUTE: |
| 97 | return "--x" ; |
| 98 | |
| 99 | case WRITE: |
| 100 | return "-w-" ; |
| 101 | |
| 102 | case WRITE_EXECUTE: |
| 103 | return "-wx" ; |
| 104 | |
| 105 | case READ: |
| 106 | return "r--" ; |
| 107 | |
| 108 | case READ_EXECUTE: |
| 109 | return "r-x" ; |
| 110 | |
| 111 | case READ_WRITE: |
| 112 | return "rw-" ; |
| 113 | |
| 114 | case ALL: |
| 115 | return "rwx" ; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Permission is used to describe a file permission. |
| 121 | */ |
| 122 | class Permission { |
| 123 | public: |
| 124 | /** |
| 125 | * To construct a Permission. |
| 126 | * @param u owner permission. |
| 127 | * @param g group permission. |
| 128 | * @param o other user permission. |
| 129 | */ |
| 130 | Permission(const Action & u, const Action & g, const Action & o) : |
| 131 | userAction(u), groupAction(g), otherAction(o), stickyBit(false) { |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * To construct a Permission from a uint16. |
| 136 | * @param mode permission flag. |
| 137 | */ |
| 138 | Permission(uint16_t mode); |
| 139 | |
| 140 | public: |
| 141 | /** |
| 142 | * To get group permission |
| 143 | * @return the group permission |
| 144 | */ |
| 145 | Action getGroupAction() const { |
| 146 | return groupAction; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * To set group permission |
| 151 | * @param groupAction the group permission |
| 152 | */ |
| 153 | void setGroupAction(Action groupAction) { |
| 154 | this->groupAction = groupAction; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * To get other user permission |
| 159 | * @return other user permission |
| 160 | */ |
| 161 | Action getOtherAction() const { |
| 162 | return otherAction; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * To set other user permission |
| 167 | * @param otherAction other user permission |
| 168 | */ |
| 169 | void setOtherAction(Action otherAction) { |
| 170 | this->otherAction = otherAction; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * To get owner permission |
| 175 | * @return the owner permission |
| 176 | */ |
| 177 | Action getUserAction() const { |
| 178 | return userAction; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * To set owner permission |
| 183 | * @param userAction the owner permission |
| 184 | */ |
| 185 | void setUserAction(Action userAction) { |
| 186 | this->userAction = userAction; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * To convert a Permission to a readable string |
| 191 | * @return a readable string |
| 192 | */ |
| 193 | std::string toString() const { |
| 194 | return Hdfs::toString(userAction) + Hdfs::toString(groupAction) |
| 195 | + Hdfs::toString(otherAction); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * To convert a Permission to a uint16 flag |
| 200 | * @return a uint16 flag |
| 201 | */ |
| 202 | uint16_t toShort() const { |
| 203 | return (uint16_t)((((uint16_t) userAction) << 6) |
| 204 | + (((uint16_t) groupAction) << 3) + (((uint16_t) otherAction)) |
| 205 | + ((stickyBit ? 1 << 9 : 0))); |
| 206 | } |
| 207 | |
| 208 | bool operator ==(const Permission & other) const { |
| 209 | return userAction == other.userAction |
| 210 | && groupAction == other.groupAction |
| 211 | && otherAction == other.otherAction |
| 212 | && stickyBit == other.stickyBit; |
| 213 | } |
| 214 | |
| 215 | private: |
| 216 | Action userAction; |
| 217 | Action groupAction; |
| 218 | Action otherAction; |
| 219 | |
| 220 | bool stickyBit; |
| 221 | }; |
| 222 | |
| 223 | } |
| 224 | #endif /* _HDFS_LIBHDFS3_CLIENT_PERMISSION_H_ */ |
| 225 | |