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   * @return null if the local name is null;
033   *         otherwise, return the local name byte array.
034   */
035  public byte[] getLocalNameBytes();
036
037  /** @return the user name. */
038  public String getUserName();
039
040  /** @return the group name. */
041  public String getGroupName();
042  
043  /** @return the permission. */
044  public FsPermission getFsPermission();
045
046  /** @return the permission as a short. */
047  public short getFsPermissionShort();
048  
049  /** @return the permission information as a long. */
050  public long getPermissionLong();
051
052  /** @return the ACL feature. */
053  public AclFeature getAclFeature();
054  
055  /** @return the XAttrs feature. */
056  public XAttrFeature getXAttrFeature();
057
058  /** @return the modification time. */
059  public long getModificationTime();
060
061  /** @return the access time. */
062  public long getAccessTime();
063
064  /** A read-only copy of the inode attributes. */
065  public static abstract class SnapshotCopy implements INodeAttributes {
066    private final byte[] name;
067    private final long permission;
068    private final AclFeature aclFeature;
069    private final long modificationTime;
070    private final long accessTime;
071    private XAttrFeature xAttrFeature;
072
073    SnapshotCopy(byte[] name, PermissionStatus permissions,
074        AclFeature aclFeature, long modificationTime, long accessTime, 
075        XAttrFeature xAttrFeature) {
076      this.name = name;
077      this.permission = PermissionStatusFormat.toLong(permissions);
078      this.aclFeature = aclFeature;
079      this.modificationTime = modificationTime;
080      this.accessTime = accessTime;
081      this.xAttrFeature = xAttrFeature;
082    }
083
084    SnapshotCopy(INode inode) {
085      this.name = inode.getLocalNameBytes();
086      this.permission = inode.getPermissionLong();
087      this.aclFeature = inode.getAclFeature();
088      this.modificationTime = inode.getModificationTime();
089      this.accessTime = inode.getAccessTime();
090      this.xAttrFeature = inode.getXAttrFeature();
091    }
092
093    @Override
094    public final byte[] getLocalNameBytes() {
095      return name;
096    }
097
098    @Override
099    public final String getUserName() {
100      final int n = (int)PermissionStatusFormat.USER.retrieve(permission);
101      return SerialNumberManager.INSTANCE.getUser(n);
102    }
103
104    @Override
105    public final String getGroupName() {
106      final int n = (int)PermissionStatusFormat.GROUP.retrieve(permission);
107      return SerialNumberManager.INSTANCE.getGroup(n);
108    }
109
110    @Override
111    public final FsPermission getFsPermission() {
112      return new FsPermission(getFsPermissionShort());
113    }
114
115    @Override
116    public final short getFsPermissionShort() {
117      return (short)PermissionStatusFormat.MODE.retrieve(permission);
118    }
119    
120    @Override
121    public long getPermissionLong() {
122      return permission;
123    }
124
125    @Override
126    public AclFeature getAclFeature() {
127      return aclFeature;
128    }
129
130    @Override
131    public final long getModificationTime() {
132      return modificationTime;
133    }
134
135    @Override
136    public final long getAccessTime() {
137      return accessTime;
138    }
139    
140    @Override
141    public final XAttrFeature getXAttrFeature() {
142      return xAttrFeature;
143    }
144  }
145}