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;
019
020import java.util.Arrays;
021
022import org.apache.commons.lang.builder.EqualsBuilder;
023import org.apache.commons.lang.builder.HashCodeBuilder;
024import 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. Four
030 * namespaces are defined: user, trusted, security and system.
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 * <p/>
047 * @see <a href="http://en.wikipedia.org/wiki/Extended_file_attributes">
048 * http://en.wikipedia.org/wiki/Extended_file_attributes</a>
049 *
050 */
051@InterfaceAudience.Private
052public class XAttr {
053
054  public static enum NameSpace {
055    USER,
056    TRUSTED,
057    SECURITY,
058    SYSTEM;
059  }
060  
061  private final NameSpace ns;
062  private final String name;
063  private final byte[] value;
064  
065  public static class Builder {
066    private NameSpace ns = NameSpace.USER;
067    private String name;
068    private byte[] value;
069    
070    public Builder setNameSpace(NameSpace ns) {
071      this.ns = ns;
072      return this;
073    }
074    
075    public Builder setName(String name) {
076      this.name = name;
077      return this;
078    }
079    
080    public Builder setValue(byte[] value) {
081      this.value = value;
082      return this;
083    }
084    
085    public XAttr build() {
086      return new XAttr(ns, name, value);
087    }
088  }
089  
090  private XAttr(NameSpace ns, String name, byte[] value) {
091    this.ns = ns;
092    this.name = name;
093    this.value = value;
094  }
095  
096  public NameSpace getNameSpace() {
097    return ns;
098  }
099  
100  public String getName() {
101    return name;
102  }
103  
104  public byte[] getValue() {
105    return value;
106  }
107  
108  @Override
109  public int hashCode() {
110    return new HashCodeBuilder(811, 67)
111        .append(name)
112        .append(ns)
113        .append(value)
114        .toHashCode();
115  }
116
117  @Override
118  public boolean equals(Object obj) {
119    if (obj == null) { return false; }
120    if (obj == this) { return true; }
121    if (obj.getClass() != getClass()) {
122      return false;
123    }
124    XAttr rhs = (XAttr) obj;
125    return new EqualsBuilder()
126        .append(ns, rhs.ns)
127        .append(name, rhs.name)
128        .append(value, rhs.value)
129        .isEquals();
130  }
131
132  /**
133   * Similar to {@link #equals(Object)}, except ignores the XAttr value.
134   *
135   * @param obj to compare equality
136   * @return if the XAttrs are equal, ignoring the XAttr value
137   */
138  public boolean equalsIgnoreValue(Object obj) {
139    if (obj == null) { return false; }
140    if (obj == this) { return true; }
141    if (obj.getClass() != getClass()) {
142      return false;
143    }
144    XAttr rhs = (XAttr) obj;
145    return new EqualsBuilder()
146        .append(ns, rhs.ns)
147        .append(name, rhs.name)
148        .isEquals();
149  }
150
151  @Override
152  public String toString() {
153    return "XAttr [ns=" + ns + ", name=" + name + ", value="
154        + Arrays.toString(value) + "]";
155  }
156}