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.hdfs.server.namenode;
019
020import org.apache.hadoop.classification.InterfaceAudience;
021import org.apache.hadoop.fs.permission.FsPermission;
022import org.apache.hadoop.fs.permission.PermissionStatus;
023import org.apache.hadoop.hdfs.server.namenode.INodeWithAdditionalFields.PermissionStatusFormat;
024import org.apache.hadoop.hdfs.server.namenode.XAttrFeature;
025
026/**
027 * The attributes of an inode.
028 */
029@InterfaceAudience.Private
030public interface INodeAttributes {
031
032  public boolean isDirectory();
033
034  /**
035   * @return null if the local name is null;
036   *         otherwise, return the local name byte array.
037   */
038  public byte[] getLocalNameBytes();
039
040  /** @return the user name. */
041  public String getUserName();
042
043  /** @return the group name. */
044  public String getGroupName();
045  
046  /** @return the permission. */
047  public FsPermission getFsPermission();
048
049  /** @return the permission as a short. */
050  public short getFsPermissionShort();
051  
052  /** @return the permission information as a long. */
053  public long getPermissionLong();
054
055  /** @return the ACL feature. */
056  public AclFeature getAclFeature();
057  
058  /** @return the XAttrs feature. */
059  public XAttrFeature getXAttrFeature();
060
061  /** @return the modification time. */
062  public long getModificationTime();
063
064  /** @return the access time. */
065  public long getAccessTime();
066
067  /** A read-only copy of the inode attributes. */
068  public static abstract class SnapshotCopy implements INodeAttributes {
069    private final byte[] name;
070    private final long permission;
071    private final AclFeature aclFeature;
072    private final long modificationTime;
073    private final long accessTime;
074    private XAttrFeature xAttrFeature;
075
076    SnapshotCopy(byte[] name, PermissionStatus permissions,
077        AclFeature aclFeature, long modificationTime, long accessTime, 
078        XAttrFeature xAttrFeature) {
079      this.name = name;
080      this.permission = PermissionStatusFormat.toLong(permissions);
081      if (aclFeature != null) {
082        aclFeature = AclStorage.addAclFeature(aclFeature);
083      }
084      this.aclFeature = aclFeature;
085      this.modificationTime = modificationTime;
086      this.accessTime = accessTime;
087      this.xAttrFeature = xAttrFeature;
088    }
089
090    SnapshotCopy(INode inode) {
091      this.name = inode.getLocalNameBytes();
092      this.permission = inode.getPermissionLong();
093      if (inode.getAclFeature() != null) {
094        aclFeature = AclStorage.addAclFeature(inode.getAclFeature());
095      } else {
096        aclFeature = null;
097      }
098      this.modificationTime = inode.getModificationTime();
099      this.accessTime = inode.getAccessTime();
100      this.xAttrFeature = inode.getXAttrFeature();
101    }
102
103    @Override
104    public final byte[] getLocalNameBytes() {
105      return name;
106    }
107
108    @Override
109    public final String getUserName() {
110      return PermissionStatusFormat.getUser(permission);
111    }
112
113    @Override
114    public final String getGroupName() {
115      return PermissionStatusFormat.getGroup(permission);
116    }
117
118    @Override
119    public final FsPermission getFsPermission() {
120      return new FsPermission(getFsPermissionShort());
121    }
122
123    @Override
124    public final short getFsPermissionShort() {
125      return PermissionStatusFormat.getMode(permission);
126    }
127    
128    @Override
129    public long getPermissionLong() {
130      return permission;
131    }
132
133    @Override
134    public AclFeature getAclFeature() {
135      return aclFeature;
136    }
137
138    @Override
139    public final long getModificationTime() {
140      return modificationTime;
141    }
142
143    @Override
144    public final long getAccessTime() {
145      return accessTime;
146    }
147    
148    @Override
149    public final XAttrFeature getXAttrFeature() {
150      return xAttrFeature;
151    }
152  }
153}