Exemple #1
0
        /// <summary>Validates the toString method for FileStatus.</summary>
        /// <param name="fileStatus">FileStatus to be validated</param>
        /// <exception cref="System.IO.IOException"/>
        private void ValidateToString(FileStatus fileStatus)
        {
            StringBuilder expected = new StringBuilder();

            expected.Append("FileStatus{");
            expected.Append("path=").Append(fileStatus.GetPath()).Append("; ");
            expected.Append("isDirectory=").Append(fileStatus.IsDirectory()).Append("; ");
            if (!fileStatus.IsDirectory())
            {
                expected.Append("length=").Append(fileStatus.GetLen()).Append("; ");
                expected.Append("replication=").Append(fileStatus.GetReplication()).Append("; ");
                expected.Append("blocksize=").Append(fileStatus.GetBlockSize()).Append("; ");
            }
            expected.Append("modification_time=").Append(fileStatus.GetModificationTime()).Append
                ("; ");
            expected.Append("access_time=").Append(fileStatus.GetAccessTime()).Append("; ");
            expected.Append("owner=").Append(fileStatus.GetOwner()).Append("; ");
            expected.Append("group=").Append(fileStatus.GetGroup()).Append("; ");
            expected.Append("permission=").Append(fileStatus.GetPermission()).Append("; ");
            if (fileStatus.IsSymlink())
            {
                expected.Append("isSymlink=").Append(true).Append("; ");
                expected.Append("symlink=").Append(fileStatus.GetSymlink()).Append("}");
            }
            else
            {
                expected.Append("isSymlink=").Append(false).Append("}");
            }
            Assert.Equal(expected.ToString(), fileStatus.ToString());
        }
Exemple #2
0
        private FileStatus DeprecatedGetFileLinkStatusInternal(Path f)
        {
            string target = FileUtil.ReadLink(new FilePath(f.ToString()));

            try
            {
                FileStatus fs = GetFileStatus(f);
                // If f refers to a regular file or directory
                if (target.IsEmpty())
                {
                    return(fs);
                }
                // Otherwise f refers to a symlink
                return(new FileStatus(fs.GetLen(), false, fs.GetReplication(), fs.GetBlockSize(),
                                      fs.GetModificationTime(), fs.GetAccessTime(), fs.GetPermission(), fs.GetOwner(),
                                      fs.GetGroup(), new Path(target), f));
            }
            catch (FileNotFoundException e)
            {
                /* The exists method in the File class returns false for dangling
                 * links so we can get a FileNotFoundException for links that exist.
                 * It's also possible that we raced with a delete of the link. Use
                 * the readBasicFileAttributes method in java.nio.file.attributes
                 * when available.
                 */
                if (!target.IsEmpty())
                {
                    return(new FileStatus(0, false, 0, 0, 0, 0, FsPermission.GetDefault(), string.Empty
                                          , string.Empty, new Path(target), f));
                }
                // f refers to a file or directory that does not exist
                throw;
            }
        }
 /// <summary>Constructor</summary>
 /// <param name="stat">a file status</param>
 /// <param name="locations">a file's block locations</param>
 /// <exception cref="System.IO.IOException"/>
 public LocatedFileStatus(FileStatus stat, BlockLocation[] locations)
     : this(stat.GetLen(), stat.IsDirectory(), stat.GetReplication(), stat.GetBlockSize
                (), stat.GetModificationTime(), stat.GetAccessTime(), stat.GetPermission(), stat
            .GetOwner(), stat.GetGroup(), null, stat.GetPath(), locations)
 {
     if (stat.IsSymlink())
     {
         SetSymlink(stat.GetSymlink());
     }
 }
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestSetPermissionAffectsTarget()
        {
            Path file       = new Path(TestBaseDir1(), "file");
            Path dir        = new Path(TestBaseDir2());
            Path linkToFile = new Path(TestBaseDir1(), "linkToFile");
            Path linkToDir  = new Path(TestBaseDir1(), "linkToDir");

            CreateAndWriteFile(file);
            wrapper.CreateSymlink(file, linkToFile, false);
            wrapper.CreateSymlink(dir, linkToDir, false);
            // Changing the permissions using the link does not modify
            // the permissions of the link..
            FsPermission perms = wrapper.GetFileLinkStatus(linkToFile).GetPermission();

            wrapper.SetPermission(linkToFile, new FsPermission((short)0x1b4));
            wrapper.SetOwner(linkToFile, "user", "group");
            NUnit.Framework.Assert.AreEqual(perms, wrapper.GetFileLinkStatus(linkToFile).GetPermission
                                                ());
            // but the file's permissions were adjusted appropriately
            FileStatus stat = wrapper.GetFileStatus(file);

            NUnit.Framework.Assert.AreEqual(0x1b4, stat.GetPermission().ToShort());
            NUnit.Framework.Assert.AreEqual("user", stat.GetOwner());
            NUnit.Framework.Assert.AreEqual("group", stat.GetGroup());
            // Getting the file's permissions via the link is the same
            // as getting the permissions directly.
            NUnit.Framework.Assert.AreEqual(stat.GetPermission(), wrapper.GetFileStatus(linkToFile
                                                                                        ).GetPermission());
            // Ditto for a link to a directory
            perms = wrapper.GetFileLinkStatus(linkToDir).GetPermission();
            wrapper.SetPermission(linkToDir, new FsPermission((short)0x1b4));
            wrapper.SetOwner(linkToDir, "user", "group");
            NUnit.Framework.Assert.AreEqual(perms, wrapper.GetFileLinkStatus(linkToDir).GetPermission
                                                ());
            stat = wrapper.GetFileStatus(dir);
            NUnit.Framework.Assert.AreEqual(0x1b4, stat.GetPermission().ToShort());
            NUnit.Framework.Assert.AreEqual("user", stat.GetOwner());
            NUnit.Framework.Assert.AreEqual("group", stat.GetGroup());
            NUnit.Framework.Assert.AreEqual(stat.GetPermission(), wrapper.GetFileStatus(linkToDir
                                                                                        ).GetPermission());
        }
        // Expected
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestDanglingLink()
        {
            Assume.AssumeTrue(!Path.Windows);
            Path fileAbs  = new Path(TestBaseDir1() + "/file");
            Path fileQual = new Path(TestURI().ToString(), fileAbs);
            Path link     = new Path(TestBaseDir1() + "/linkToFile");
            Path linkQual = new Path(TestURI().ToString(), link.ToString());

            wrapper.CreateSymlink(fileAbs, link, false);
            // Deleting the link using FileContext currently fails because
            // resolve looks up LocalFs rather than RawLocalFs for the path
            // so we call ChecksumFs delete (which doesn't delete dangling
            // links) instead of delegating to delete in RawLocalFileSystem
            // which deletes via fullyDelete. testDeleteLink above works
            // because the link is not dangling.
            //assertTrue(fc.delete(link, false));
            FileUtil.FullyDelete(new FilePath(link.ToUri().GetPath()));
            wrapper.CreateSymlink(fileAbs, link, false);
            try
            {
                wrapper.GetFileStatus(link);
                NUnit.Framework.Assert.Fail("Got FileStatus for dangling link");
            }
            catch (FileNotFoundException)
            {
            }
            // Expected. File's exists method returns false for dangling links
            // We can stat a dangling link
            UserGroupInformation user = UserGroupInformation.GetCurrentUser();
            FileStatus           fsd  = wrapper.GetFileLinkStatus(link);

            Assert.Equal(fileQual, fsd.GetSymlink());
            Assert.True(fsd.IsSymlink());
            NUnit.Framework.Assert.IsFalse(fsd.IsDirectory());
            Assert.Equal(user.GetUserName(), fsd.GetOwner());
            // Compare against user's primary group
            Assert.Equal(user.GetGroupNames()[0], fsd.GetGroup());
            Assert.Equal(linkQual, fsd.GetPath());
            // Accessing the link
            try
            {
                ReadFile(link);
                NUnit.Framework.Assert.Fail("Got FileStatus for dangling link");
            }
            catch (FileNotFoundException)
            {
            }
            // Ditto.
            // Creating the file makes the link work
            CreateAndWriteFile(fileAbs);
            wrapper.GetFileStatus(link);
        }
Exemple #6
0
 /// <summary>Validate the accessors for FileStatus.</summary>
 /// <param name="fileStatus">FileStatus to checked</param>
 /// <param name="length">expected length</param>
 /// <param name="isdir">expected isDirectory</param>
 /// <param name="replication">expected replication</param>
 /// <param name="blocksize">expected blocksize</param>
 /// <param name="mtime">expected modification time</param>
 /// <param name="atime">expected access time</param>
 /// <param name="permission">expected permission</param>
 /// <param name="owner">expected owner</param>
 /// <param name="group">expected group</param>
 /// <param name="symlink">expected symlink</param>
 /// <param name="path">expected path</param>
 /// <exception cref="System.IO.IOException"/>
 private void ValidateAccessors(FileStatus fileStatus, long length, bool isdir, int
                                replication, long blocksize, long mtime, long atime, FsPermission permission, string
                                owner, string group, Path symlink, Path path)
 {
     Assert.Equal(length, fileStatus.GetLen());
     Assert.Equal(isdir, fileStatus.IsDirectory());
     Assert.Equal(replication, fileStatus.GetReplication());
     Assert.Equal(blocksize, fileStatus.GetBlockSize());
     Assert.Equal(mtime, fileStatus.GetModificationTime());
     Assert.Equal(atime, fileStatus.GetAccessTime());
     Assert.Equal(permission, fileStatus.GetPermission());
     Assert.Equal(owner, fileStatus.GetOwner());
     Assert.Equal(group, fileStatus.GetGroup());
     if (symlink == null)
     {
         NUnit.Framework.Assert.IsFalse(fileStatus.IsSymlink());
     }
     else
     {
         Assert.True(fileStatus.IsSymlink());
         Assert.Equal(symlink, fileStatus.GetSymlink());
     }
     Assert.Equal(path, fileStatus.GetPath());
 }