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