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;
019
020 import java.util.Arrays;
021
022 import org.apache.commons.lang.builder.EqualsBuilder;
023 import org.apache.commons.lang.builder.HashCodeBuilder;
024 import org.apache.hadoop.classification.InterfaceAudience;
025
026 /**
027 * XAttr is the POSIX Extended Attribute model similar to that found in
028 * traditional Operating Systems. Extended Attributes consist of one
029 * or more name/value pairs associated with a file or directory. Five
030 * namespaces are defined: user, trusted, security, system and raw.
031 * 1) USER namespace attributes may be used by any user to store
032 * arbitrary information. Access permissions in this namespace are
033 * defined by a file directory's permission bits. For sticky directories,
034 * only the owner and privileged user can write attributes.
035 * <br>
036 * 2) TRUSTED namespace attributes are only visible and accessible to
037 * privileged users. This namespace is available from both user space
038 * (filesystem API) and fs kernel.
039 * <br>
040 * 3) SYSTEM namespace attributes are used by the fs kernel to store
041 * system objects. This namespace is only available in the fs
042 * kernel. It is not visible to users.
043 * <br>
044 * 4) SECURITY namespace attributes are used by the fs kernel for
045 * security features. It is not visible to users.
046 * <br>
047 * 5) RAW namespace attributes are used for internal system attributes that
048 * sometimes need to be exposed. Like SYSTEM namespace attributes they are
049 * not visible to the user except when getXAttr/getXAttrs is called on a file
050 * or directory in the /.reserved/raw HDFS directory hierarchy. These
051 * attributes can only be accessed by the superuser.
052 * <p/>
053 * @see <a href="http://en.wikipedia.org/wiki/Extended_file_attributes">
054 * http://en.wikipedia.org/wiki/Extended_file_attributes</a>
055 *
056 */
057 @InterfaceAudience.Private
058 public class XAttr {
059
060 public static enum NameSpace {
061 USER,
062 TRUSTED,
063 SECURITY,
064 SYSTEM,
065 RAW;
066 }
067
068 private final NameSpace ns;
069 private final String name;
070 private final byte[] value;
071
072 public static class Builder {
073 private NameSpace ns = NameSpace.USER;
074 private String name;
075 private byte[] value;
076
077 public Builder setNameSpace(NameSpace ns) {
078 this.ns = ns;
079 return this;
080 }
081
082 public Builder setName(String name) {
083 this.name = name;
084 return this;
085 }
086
087 public Builder setValue(byte[] value) {
088 this.value = value;
089 return this;
090 }
091
092 public XAttr build() {
093 return new XAttr(ns, name, value);
094 }
095 }
096
097 private XAttr(NameSpace ns, String name, byte[] value) {
098 this.ns = ns;
099 this.name = name;
100 this.value = value;
101 }
102
103 public NameSpace getNameSpace() {
104 return ns;
105 }
106
107 public String getName() {
108 return name;
109 }
110
111 public byte[] getValue() {
112 return value;
113 }
114
115 @Override
116 public int hashCode() {
117 return new HashCodeBuilder(811, 67)
118 .append(name)
119 .append(ns)
120 .append(value)
121 .toHashCode();
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (obj == null) { return false; }
127 if (obj == this) { return true; }
128 if (obj.getClass() != getClass()) {
129 return false;
130 }
131 XAttr rhs = (XAttr) obj;
132 return new EqualsBuilder()
133 .append(ns, rhs.ns)
134 .append(name, rhs.name)
135 .append(value, rhs.value)
136 .isEquals();
137 }
138
139 /**
140 * Similar to {@link #equals(Object)}, except ignores the XAttr value.
141 *
142 * @param obj to compare equality
143 * @return if the XAttrs are equal, ignoring the XAttr value
144 */
145 public boolean equalsIgnoreValue(Object obj) {
146 if (obj == null) { return false; }
147 if (obj == this) { return true; }
148 if (obj.getClass() != getClass()) {
149 return false;
150 }
151 XAttr rhs = (XAttr) obj;
152 return new EqualsBuilder()
153 .append(ns, rhs.ns)
154 .append(name, rhs.name)
155 .isEquals();
156 }
157
158 @Override
159 public String toString() {
160 return "XAttr [ns=" + ns + ", name=" + name + ", value="
161 + Arrays.toString(value) + "]";
162 }
163 }