Пример #1
0
        /// <exception cref="System.IO.IOException"/>
        internal static bool SetReplication(FSDirectory fsd, BlockManager bm, string src,
                                            short replication)
        {
            bm.VerifyReplication(src, replication, null);
            bool isFile;
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            fsd.WriteLock();
            try
            {
                src = fsd.ResolvePath(pc, src, pathComponents);
                INodesInPath iip = fsd.GetINodesInPath4Write(src);
                if (fsd.IsPermissionEnabled())
                {
                    fsd.CheckPathAccess(pc, iip, FsAction.Write);
                }
                short[] blockRepls = new short[2];
                // 0: old, 1: new
                Block[] blocks = UnprotectedSetReplication(fsd, src, replication, blockRepls);
                isFile = blocks != null;
                if (isFile)
                {
                    fsd.GetEditLog().LogSetReplication(src, replication);
                    bm.SetReplication(blockRepls[0], blockRepls[1], src, blocks);
                }
            }
            finally
            {
                fsd.WriteUnlock();
            }
            return(isFile);
        }
Пример #2
0
        /// <exception cref="System.IO.IOException"/>
        internal static IList <XAttr> GetXAttrs(FSDirectory fsd, string srcArg, IList <XAttr
                                                                                       > xAttrs)
        {
            string src = srcArg;

            CheckXAttrsConfigFlag(fsd);
            FSPermissionChecker pc = fsd.GetPermissionChecker();
            bool isRawPath         = FSDirectory.IsReservedRawName(src);
            bool getAll            = xAttrs == null || xAttrs.IsEmpty();

            if (!getAll)
            {
                XAttrPermissionFilter.CheckPermissionForApi(pc, xAttrs, isRawPath);
            }
            byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            src = fsd.ResolvePath(pc, src, pathComponents);
            INodesInPath iip = fsd.GetINodesInPath(src, true);

            if (fsd.IsPermissionEnabled())
            {
                fsd.CheckPathAccess(pc, iip, FsAction.Read);
            }
            IList <XAttr> all         = FSDirXAttrOp.GetXAttrs(fsd, src);
            IList <XAttr> filteredAll = XAttrPermissionFilter.FilterXAttrsForApi(pc, all, isRawPath
                                                                                 );

            if (getAll)
            {
                return(filteredAll);
            }
            if (filteredAll == null || filteredAll.IsEmpty())
            {
                return(null);
            }
            IList <XAttr> toGet = Lists.NewArrayListWithCapacity(xAttrs.Count);

            foreach (XAttr xAttr in xAttrs)
            {
                bool foundIt = false;
                foreach (XAttr a in filteredAll)
                {
                    if (xAttr.GetNameSpace() == a.GetNameSpace() && xAttr.GetName().Equals(a.GetName(
                                                                                               )))
                    {
                        toGet.AddItem(a);
                        foundIt = true;
                        break;
                    }
                }
                if (!foundIt)
                {
                    throw new IOException("At least one of the attributes provided was not found.");
                }
            }
            return(toGet);
        }
Пример #3
0
        /// <exception cref="System.IO.IOException"/>
        internal static DirectoryListing GetListingInt(FSDirectory fsd, string srcArg, byte
                                                       [] startAfter, bool needLocation)
        {
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            byte[][]     pathComponents   = FSDirectory.GetPathComponentsForReservedPath(srcArg);
            string       startAfterString = new string(startAfter, Charsets.Utf8);
            string       src = fsd.ResolvePath(pc, srcArg, pathComponents);
            INodesInPath iip = fsd.GetINodesInPath(src, true);

            // Get file name when startAfter is an INodePath
            if (FSDirectory.IsReservedName(startAfterString))
            {
                byte[][] startAfterComponents = FSDirectory.GetPathComponentsForReservedPath(startAfterString
                                                                                             );
                try
                {
                    string   tmp         = FSDirectory.ResolvePath(src, startAfterComponents, fsd);
                    byte[][] regularPath = INode.GetPathComponents(tmp);
                    startAfter = regularPath[regularPath.Length - 1];
                }
                catch (IOException)
                {
                    // Possibly the inode is deleted
                    throw new DirectoryListingStartAfterNotFoundException("Can't find startAfter " +
                                                                          startAfterString);
                }
            }
            bool isSuperUser = true;

            if (fsd.IsPermissionEnabled())
            {
                if (iip.GetLastINode() != null && iip.GetLastINode().IsDirectory())
                {
                    fsd.CheckPathAccess(pc, iip, FsAction.ReadExecute);
                }
                else
                {
                    fsd.CheckTraverse(pc, iip);
                }
                isSuperUser = pc.IsSuperUser();
            }
            return(GetListing(fsd, iip, src, startAfter, needLocation, isSuperUser));
        }
Пример #4
0
        /// <exception cref="System.IO.IOException"/>
        internal static HdfsFileStatus SetTimes(FSDirectory fsd, string src, long mtime,
                                                long atime)
        {
            if (!fsd.IsAccessTimeSupported() && atime != -1)
            {
                throw new IOException("Access time for hdfs is not configured. " + " Please set "
                                      + DFSConfigKeys.DfsNamenodeAccesstimePrecisionKey + " configuration parameter."
                                      );
            }
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            byte[][]     pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            INodesInPath iip;

            fsd.WriteLock();
            try
            {
                src = fsd.ResolvePath(pc, src, pathComponents);
                iip = fsd.GetINodesInPath4Write(src);
                // Write access is required to set access and modification times
                if (fsd.IsPermissionEnabled())
                {
                    fsd.CheckPathAccess(pc, iip, FsAction.Write);
                }
                INode inode = iip.GetLastINode();
                if (inode == null)
                {
                    throw new FileNotFoundException("File/Directory " + src + " does not exist.");
                }
                bool changed = UnprotectedSetTimes(fsd, inode, mtime, atime, true, iip.GetLatestSnapshotId
                                                       ());
                if (changed)
                {
                    fsd.GetEditLog().LogTimes(src, mtime, atime);
                }
            }
            finally
            {
                fsd.WriteUnlock();
            }
            return(fsd.GetAuditFileInfo(iip));
        }
Пример #5
0
        /// <exception cref="System.IO.IOException"/>
        internal static HdfsFileStatus Concat(FSDirectory fsd, string target, string[] srcs
                                              , bool logRetryCache)
        {
            Preconditions.CheckArgument(!target.IsEmpty(), "Target file name is empty");
            Preconditions.CheckArgument(srcs != null && srcs.Length > 0, "No sources given");
            System.Diagnostics.Debug.Assert(srcs != null);
            if (FSDirectory.Log.IsDebugEnabled())
            {
                FSDirectory.Log.Debug("concat {} to {}", Arrays.ToString(srcs), target);
            }
            INodesInPath targetIIP = fsd.GetINodesInPath4Write(target);
            // write permission for the target
            FSPermissionChecker pc = null;

            if (fsd.IsPermissionEnabled())
            {
                pc = fsd.GetPermissionChecker();
                fsd.CheckPathAccess(pc, targetIIP, FsAction.Write);
            }
            // check the target
            VerifyTargetFile(fsd, target, targetIIP);
            // check the srcs
            INodeFile[] srcFiles = VerifySrcFiles(fsd, srcs, targetIIP, pc);
            if (NameNode.stateChangeLog.IsDebugEnabled())
            {
                NameNode.stateChangeLog.Debug("DIR* NameSystem.concat: " + Arrays.ToString(srcs)
                                              + " to " + target);
            }
            long timestamp = Time.Now();

            fsd.WriteLock();
            try
            {
                UnprotectedConcat(fsd, targetIIP, srcFiles, timestamp);
            }
            finally
            {
                fsd.WriteUnlock();
            }
            fsd.GetEditLog().LogConcat(target, srcs, timestamp, logRetryCache);
            return(fsd.GetAuditFileInfo(targetIIP));
        }
Пример #6
0
 /// <exception cref="Org.Apache.Hadoop.Security.AccessControlException"/>
 private static void CheckXAttrChangeAccess(FSDirectory fsd, INodesInPath iip, XAttr
                                            xAttr, FSPermissionChecker pc)
 {
     if (fsd.IsPermissionEnabled() && xAttr.GetNameSpace() == XAttr.NameSpace.User)
     {
         INode inode = iip.GetLastINode();
         if (inode != null && inode.IsDirectory() && inode.GetFsPermission().GetStickyBit(
                 ))
         {
             if (!pc.IsSuperUser())
             {
                 fsd.CheckOwner(pc, iip);
             }
         }
         else
         {
             fsd.CheckPathAccess(pc, iip, FsAction.Write);
         }
     }
 }
Пример #7
0
        /// <exception cref="System.IO.IOException"/>
        internal static HdfsFileStatus SetStoragePolicy(FSDirectory fsd, BlockManager bm,
                                                        string src, string policyName)
        {
            if (!fsd.IsStoragePolicyEnabled())
            {
                throw new IOException("Failed to set storage policy since " + DFSConfigKeys.DfsStoragePolicyEnabledKey
                                      + " is set to false.");
            }
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            byte[][]     pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            INodesInPath iip;

            fsd.WriteLock();
            try
            {
                src = FSDirectory.ResolvePath(src, pathComponents, fsd);
                iip = fsd.GetINodesInPath4Write(src);
                if (fsd.IsPermissionEnabled())
                {
                    fsd.CheckPathAccess(pc, iip, FsAction.Write);
                }
                // get the corresponding policy and make sure the policy name is valid
                BlockStoragePolicy policy = bm.GetStoragePolicy(policyName);
                if (policy == null)
                {
                    throw new HadoopIllegalArgumentException("Cannot find a block policy with the name "
                                                             + policyName);
                }
                UnprotectedSetStoragePolicy(fsd, bm, iip, policy.GetId());
                fsd.GetEditLog().LogSetStoragePolicy(src, policy.GetId());
            }
            finally
            {
                fsd.WriteUnlock();
            }
            return(fsd.GetAuditFileInfo(iip));
        }
Пример #8
0
        /// <exception cref="System.IO.IOException"/>
        private static INodeFile[] VerifySrcFiles(FSDirectory fsd, string[] srcs, INodesInPath
                                                  targetIIP, FSPermissionChecker pc)
        {
            // to make sure no two files are the same
            ICollection <INodeFile> si  = new LinkedHashSet <INodeFile>();
            INodeFile      targetINode  = targetIIP.GetLastINode().AsFile();
            INodeDirectory targetParent = targetINode.GetParent();

            // now check the srcs
            foreach (string src in srcs)
            {
                INodesInPath iip = fsd.GetINodesInPath4Write(src);
                // permission check for srcs
                if (pc != null)
                {
                    fsd.CheckPathAccess(pc, iip, FsAction.Read);
                    // read the file
                    fsd.CheckParentAccess(pc, iip, FsAction.Write);
                }
                // for delete
                INode     srcINode     = iip.GetLastINode();
                INodeFile srcINodeFile = INodeFile.ValueOf(srcINode, src);
                // make sure the src file and the target file are in the same dir
                if (srcINodeFile.GetParent() != targetParent)
                {
                    throw new HadoopIllegalArgumentException("Source file " + src + " is not in the same directory with the target "
                                                             + targetIIP.GetPath());
                }
                // make sure all the source files are not in snapshot
                if (srcINode.IsInLatestSnapshot(iip.GetLatestSnapshotId()))
                {
                    throw new SnapshotException("Concat: the source file " + src + " is in snapshot");
                }
                // check if the file has other references.
                if (srcINode.IsReference() && ((INodeReference.WithCount)srcINode.AsReference().GetReferredINode
                                                   ()).GetReferenceCount() > 1)
                {
                    throw new SnapshotException("Concat: the source file " + src + " is referred by some other reference in some snapshot."
                                                );
                }
                // source file cannot be the same with the target file
                if (srcINode == targetINode)
                {
                    throw new HadoopIllegalArgumentException("concat: the src file " + src + " is the same with the target file "
                                                             + targetIIP.GetPath());
                }
                // source file cannot be under construction or empty
                if (srcINodeFile.IsUnderConstruction() || srcINodeFile.NumBlocks() == 0)
                {
                    throw new HadoopIllegalArgumentException("concat: source file " + src + " is invalid or empty or underConstruction"
                                                             );
                }
                // source file's preferred block size cannot be greater than the target
                // file
                if (srcINodeFile.GetPreferredBlockSize() > targetINode.GetPreferredBlockSize())
                {
                    throw new HadoopIllegalArgumentException("concat: source file " + src + " has preferred block size "
                                                             + srcINodeFile.GetPreferredBlockSize() + " which is greater than the target file's preferred block size "
                                                             + targetINode.GetPreferredBlockSize());
                }
                si.AddItem(srcINodeFile);
            }
            // make sure no two files are the same
            if (si.Count < srcs.Length)
            {
                // it means at least two files are the same
                throw new HadoopIllegalArgumentException("concat: at least two of the source files are the same"
                                                         );
            }
            return(Sharpen.Collections.ToArray(si, new INodeFile[si.Count]));
        }