Esempio n. 1
0
        /// <exception cref="System.IO.IOException"/>
        internal static long GetPreferredBlockSize(FSDirectory fsd, string src)
        {
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            fsd.ReadLock();
            try
            {
                src = fsd.ResolvePath(pc, src, pathComponents);
                INodesInPath iip = fsd.GetINodesInPath(src, false);
                if (fsd.IsPermissionEnabled())
                {
                    fsd.CheckTraverse(pc, iip);
                }
                return(INodeFile.ValueOf(iip.GetLastINode(), src).GetPreferredBlockSize());
            }
            finally
            {
                fsd.ReadUnlock();
            }
        }
Esempio n. 2
0
        /// <exception cref="System.IO.IOException"/>
        internal static AclStatus GetAclStatus(FSDirectory fsd, string src)
        {
            CheckAclsConfigFlag(fsd);
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            src = fsd.ResolvePath(pc, src, pathComponents);
            string srcs = FSDirectory.NormalizePath(src);

            fsd.ReadLock();
            try
            {
                // There is no real inode for the path ending in ".snapshot", so return a
                // non-null, unpopulated AclStatus.  This is similar to getFileInfo.
                if (srcs.EndsWith(HdfsConstants.SeparatorDotSnapshotDir) && fsd.GetINode4DotSnapshot
                        (srcs) != null)
                {
                    return(new AclStatus.Builder().Owner(string.Empty).Group(string.Empty).Build());
                }
                INodesInPath iip = fsd.GetINodesInPath(srcs, true);
                if (fsd.IsPermissionEnabled())
                {
                    fsd.CheckTraverse(pc, iip);
                }
                INode            inode      = FSDirectory.ResolveLastINode(iip);
                int              snapshotId = iip.GetPathSnapshotId();
                IList <AclEntry> acl        = AclStorage.ReadINodeAcl(fsd.GetAttributes(src, inode.GetLocalNameBytes
                                                                                            (), inode, snapshotId));
                FsPermission fsPermission = inode.GetFsPermission(snapshotId);
                return(new AclStatus.Builder().Owner(inode.GetUserName()).Group(inode.GetGroupName
                                                                                    ()).StickyBit(fsPermission.GetStickyBit()).SetPermission(fsPermission).AddEntries
                           (acl).Build());
            }
            finally
            {
                fsd.ReadUnlock();
            }
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        /// <exception cref="System.IO.IOException"/>
        internal static SnapshotDiffReport GetSnapshotDiffReport(FSDirectory fsd, SnapshotManager
                                                                 snapshotManager, string path, string fromSnapshot, string toSnapshot)
        {
            SnapshotDiffReport  diffs;
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            fsd.ReadLock();
            try
            {
                if (fsd.IsPermissionEnabled())
                {
                    CheckSubtreeReadPermission(fsd, pc, path, fromSnapshot);
                    CheckSubtreeReadPermission(fsd, pc, path, toSnapshot);
                }
                INodesInPath iip = fsd.GetINodesInPath(path, true);
                diffs = snapshotManager.Diff(iip, path, fromSnapshot, toSnapshot);
            }
            finally
            {
                fsd.ReadUnlock();
            }
            return(diffs);
        }
Esempio n. 5
0
        /// <summary>Create a snapshot</summary>
        /// <param name="snapshotRoot">The directory path where the snapshot is taken</param>
        /// <param name="snapshotName">The name of the snapshot</param>
        /// <exception cref="System.IO.IOException"/>
        internal static string CreateSnapshot(FSDirectory fsd, SnapshotManager snapshotManager
                                              , string snapshotRoot, string snapshotName, bool logRetryCache)
        {
            INodesInPath iip = fsd.GetINodesInPath4Write(snapshotRoot);

            if (fsd.IsPermissionEnabled())
            {
                FSPermissionChecker pc = fsd.GetPermissionChecker();
                fsd.CheckOwner(pc, iip);
            }
            if (snapshotName == null || snapshotName.IsEmpty())
            {
                snapshotName = Org.Apache.Hadoop.Hdfs.Server.Namenode.Snapshot.Snapshot.GenerateDefaultSnapshotName
                                   ();
            }
            else
            {
                if (!DFSUtil.IsValidNameForComponent(snapshotName))
                {
                    throw new InvalidPathException("Invalid snapshot name: " + snapshotName);
                }
            }
            string snapshotPath = null;

            VerifySnapshotName(fsd, snapshotName, snapshotRoot);
            fsd.WriteLock();
            try
            {
                snapshotPath = snapshotManager.CreateSnapshot(iip, snapshotRoot, snapshotName);
            }
            finally
            {
                fsd.WriteUnlock();
            }
            fsd.GetEditLog().LogCreateSnapshot(snapshotRoot, snapshotName, logRetryCache);
            return(snapshotPath);
        }
Esempio n. 6
0
        /// <seealso>
        ///
        /// <see cref="UnprotectedRenameTo(FSDirectory, string, string, INodesInPath, INodesInPath, long, BlocksMapUpdateInfo, Org.Apache.Hadoop.FS.Options.Rename[])
        ///     "/>
        /// </seealso>
        /// <exception cref="System.IO.IOException"/>
        internal static void RenameTo(FSDirectory fsd, FSPermissionChecker pc, string src
                                      , string dst, INode.BlocksMapUpdateInfo collectedBlocks, bool logRetryCache, params
                                      Options.Rename[] options)
        {
            INodesInPath srcIIP = fsd.GetINodesInPath4Write(src, false);
            INodesInPath dstIIP = fsd.GetINodesInPath4Write(dst, false);

            if (fsd.IsPermissionEnabled())
            {
                // Rename does not operate on link targets
                // Do not resolveLink when checking permissions of src and dst
                // Check write access to parent of src
                fsd.CheckPermission(pc, srcIIP, false, null, FsAction.Write, null, null, false);
                // Check write access to ancestor of dst
                fsd.CheckPermission(pc, dstIIP, false, FsAction.Write, null, null, null, false);
            }
            if (NameNode.stateChangeLog.IsDebugEnabled())
            {
                NameNode.stateChangeLog.Debug("DIR* FSDirectory.renameTo: " + src + " to " + dst);
            }
            long mtime = Time.Now();

            fsd.WriteLock();
            try
            {
                if (UnprotectedRenameTo(fsd, src, dst, srcIIP, dstIIP, mtime, collectedBlocks, options
                                        ))
                {
                    FSDirDeleteOp.IncrDeletedFileCount(1);
                }
            }
            finally
            {
                fsd.WriteUnlock();
            }
            fsd.GetEditLog().LogRename(src, dst, mtime, logRetryCache, options);
        }
Esempio n. 7
0
        /// <exception cref="System.IO.IOException"/>
        internal static void RenameSnapshot(FSDirectory fsd, SnapshotManager snapshotManager
                                            , string path, string snapshotOldName, string snapshotNewName, bool logRetryCache
                                            )
        {
            INodesInPath iip = fsd.GetINodesInPath4Write(path);

            if (fsd.IsPermissionEnabled())
            {
                FSPermissionChecker pc = fsd.GetPermissionChecker();
                fsd.CheckOwner(pc, iip);
            }
            VerifySnapshotName(fsd, snapshotNewName, path);
            fsd.WriteLock();
            try
            {
                snapshotManager.RenameSnapshot(iip, path, snapshotOldName, snapshotNewName);
            }
            finally
            {
                fsd.WriteUnlock();
            }
            fsd.GetEditLog().LogRenameSnapshot(path, snapshotOldName, snapshotNewName, logRetryCache
                                               );
        }
        /// <summary>Get the file info for a specific file.</summary>
        /// <param name="srcArg">The string representation of the path to the file</param>
        /// <param name="resolveLink">
        /// whether to throw UnresolvedLinkException
        /// if src refers to a symlink
        /// </param>
        /// <returns>
        /// object containing information regarding the file
        /// or null if file not found
        /// </returns>
        /// <exception cref="System.IO.IOException"/>
        internal static HdfsFileStatus GetFileInfo(FSDirectory fsd, string srcArg, bool resolveLink
                                                   )
        {
            string src = srcArg;

            if (!DFSUtil.IsValidName(src))
            {
                throw new InvalidPathException("Invalid file name: " + src);
            }
            FSPermissionChecker pc = fsd.GetPermissionChecker();

            byte[][] pathComponents = FSDirectory.GetPathComponentsForReservedPath(src);
            src = fsd.ResolvePath(pc, src, pathComponents);
            INodesInPath iip         = fsd.GetINodesInPath(src, resolveLink);
            bool         isSuperUser = true;

            if (fsd.IsPermissionEnabled())
            {
                fsd.CheckPermission(pc, iip, false, null, null, null, null, false);
                isSuperUser = pc.IsSuperUser();
            }
            return(GetFileInfo(fsd, src, resolveLink, FSDirectory.IsReservedRawName(srcArg),
                               isSuperUser));
        }
Esempio n. 9
0
        /// <exception cref="System.IO.IOException"/>
        internal static HdfsFileStatus Mkdirs(FSNamesystem fsn, string src, PermissionStatus
                                              permissions, bool createParent)
        {
            FSDirectory fsd = fsn.GetFSDirectory();

            if (NameNode.stateChangeLog.IsDebugEnabled())
            {
                NameNode.stateChangeLog.Debug("DIR* NameSystem.mkdirs: " + src);
            }
            if (!DFSUtil.IsValidName(src))
            {
                throw new InvalidPathException(src);
            }
            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.CheckTraverse(pc, iip);
                }
                INode lastINode = iip.GetLastINode();
                if (lastINode != null && lastINode.IsFile())
                {
                    throw new FileAlreadyExistsException("Path is not a directory: " + src);
                }
                INodesInPath existing = lastINode != null ? iip : iip.GetExistingINodes();
                if (lastINode == null)
                {
                    if (fsd.IsPermissionEnabled())
                    {
                        fsd.CheckAncestorAccess(pc, iip, FsAction.Write);
                    }
                    if (!createParent)
                    {
                        fsd.VerifyParentDir(iip, src);
                    }
                    // validate that we have enough inodes. This is, at best, a
                    // heuristic because the mkdirs() operation might need to
                    // create multiple inodes.
                    fsn.CheckFsObjectLimit();
                    IList <string> nonExisting = iip.GetPath(existing.Length(), iip.Length() - existing
                                                             .Length());
                    int length = nonExisting.Count;
                    if (length > 1)
                    {
                        IList <string> ancestors = nonExisting.SubList(0, length - 1);
                        // Ensure that the user can traversal the path by adding implicit
                        // u+wx permission to all ancestor directories
                        existing = CreateChildrenDirectories(fsd, existing, ancestors, AddImplicitUwx(permissions
                                                                                                      , permissions));
                        if (existing == null)
                        {
                            throw new IOException("Failed to create directory: " + src);
                        }
                    }
                    if ((existing = CreateChildrenDirectories(fsd, existing, nonExisting.SubList(length
                                                                                                 - 1, length), permissions)) == null)
                    {
                        throw new IOException("Failed to create directory: " + src);
                    }
                }
                return(fsd.GetAuditFileInfo(existing));
            }
            finally
            {
                fsd.WriteUnlock();
            }
        }