public virtual void TestOldRenameWithQuota()
        {
            DistributedFileSystem fs = cluster.GetFileSystem();
            Path src1 = GetTestRootPath(fc, "test/testOldRenameWithQuota/srcdir/src1");
            Path src2 = GetTestRootPath(fc, "test/testOldRenameWithQuota/srcdir/src2");
            Path dst1 = GetTestRootPath(fc, "test/testOldRenameWithQuota/dstdir/dst1");
            Path dst2 = GetTestRootPath(fc, "test/testOldRenameWithQuota/dstdir/dst2");

            CreateFile(src1);
            CreateFile(src2);
            fs.SetQuota(src1.GetParent(), HdfsConstants.QuotaDontSet, HdfsConstants.QuotaDontSet
                        );
            fc.Mkdir(dst1.GetParent(), FileContext.DefaultPerm, true);
            fs.SetQuota(dst1.GetParent(), 2, HdfsConstants.QuotaDontSet);

            /*
             * Test1: src does not exceed quota and dst has no quota check and hence
             * accommodates rename
             */
            OldRename(src1, dst1, true, false);

            /*
             * Test2: src does not exceed quota and dst has *no* quota to accommodate
             * rename.
             */
            // dstDir quota = 1 and dst1 already uses it
            OldRename(src2, dst2, false, true);

            /*
             * Test3: src exceeds quota and dst has *no* quota to accommodate rename
             */
            // src1 has no quota to accommodate new rename node
            fs.SetQuota(src1.GetParent(), 1, HdfsConstants.QuotaDontSet);
            OldRename(dst1, src1, false, true);
        }
        public virtual void TestRenameWithQuota()
        {
            DistributedFileSystem fs = cluster.GetFileSystem();
            Path src1 = GetTestRootPath(fc, "test/testRenameWithQuota/srcdir/src1");
            Path src2 = GetTestRootPath(fc, "test/testRenameWithQuota/srcdir/src2");
            Path dst1 = GetTestRootPath(fc, "test/testRenameWithQuota/dstdir/dst1");
            Path dst2 = GetTestRootPath(fc, "test/testRenameWithQuota/dstdir/dst2");

            CreateFile(src1);
            CreateFile(src2);
            fs.SetQuota(src1.GetParent(), HdfsConstants.QuotaDontSet, HdfsConstants.QuotaDontSet
                        );
            fc.Mkdir(dst1.GetParent(), FileContext.DefaultPerm, true);
            fs.SetQuota(dst1.GetParent(), 2, HdfsConstants.QuotaDontSet);

            /*
             * Test1: src does not exceed quota and dst has no quota check and hence
             * accommodates rename
             */
            // rename uses dstdir quota=1
            Rename(src1, dst1, false, true, false, Options.Rename.None);
            // rename reuses dstdir quota=1
            Rename(src2, dst1, true, true, false, Options.Rename.Overwrite);

            /*
             * Test2: src does not exceed quota and dst has *no* quota to accommodate
             * rename.
             */
            // dstDir quota = 1 and dst1 already uses it
            CreateFile(src2);
            Rename(src2, dst2, false, false, true, Options.Rename.None);

            /*
             * Test3: src exceeds quota and dst has *no* quota to accommodate rename
             * rename to a destination that does not exist
             */
            // src1 has no quota to accommodate new rename node
            fs.SetQuota(src1.GetParent(), 1, HdfsConstants.QuotaDontSet);
            Rename(dst1, src1, false, false, true, Options.Rename.None);

            /*
             * Test4: src exceeds quota and dst has *no* quota to accommodate rename
             * rename to a destination that exists and quota freed by deletion of dst
             * is same as quota needed by src.
             */
            // src1 has no quota to accommodate new rename node
            fs.SetQuota(src1.GetParent(), 100, HdfsConstants.QuotaDontSet);
            CreateFile(src1);
            fs.SetQuota(src1.GetParent(), 1, HdfsConstants.QuotaDontSet);
            Rename(dst1, src1, true, true, false, Options.Rename.Overwrite);
        }
예제 #3
0
        public virtual void TestCreateFileInNonExistingDirectory()
        {
            string fileName = "testDir/testFile";
            Path   testPath = QualifiedPath(fileName, fc2);

            // Ensure file does not exist
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.Exists(fc2, testPath));
            // Create a file on fc2's file system using fc1
            FileContextTestHelper.CreateFile(fc1, testPath);
            // Ensure using fc2 that file is created
            Assert.True(FileContextTestHelper.IsDir(fc2, testPath.GetParent
                                                        ()));
            Assert.Equal("testDir", testPath.GetParent().GetName());
            Assert.True(FileContextTestHelper.Exists(fc2, testPath));
        }
예제 #4
0
 /// <exception cref="System.IO.IOException"/>
 public override FSDataOutputStream CreateInternal(Path f, EnumSet <CreateFlag> flag
                                                   , FsPermission absolutePermission, int bufferSize, short replication, long blockSize
                                                   , Progressable progress, Options.ChecksumOpt checksumOpt, bool createParent)
 {
     // call to primitiveCreate
     CheckPath(f);
     // Default impl assumes that permissions do not matter
     // calling the regular create is good enough.
     // FSs that implement permissions should override this.
     if (!createParent)
     {
         // parent must exist.
         // since this.create makes parent dirs automatically
         // we must throw exception if parent does not exist.
         FileStatus stat = GetFileStatus(f.GetParent());
         if (stat == null)
         {
             throw new FileNotFoundException("Missing parent:" + f);
         }
         if (!stat.IsDirectory())
         {
             throw new ParentNotDirectoryException("parent is not a dir:" + f);
         }
     }
     // parent does exist - go ahead with create of file.
     return(fsImpl.PrimitiveCreate(f, absolutePermission, flag, bufferSize, replication
                                   , blockSize, progress, checksumOpt));
 }
예제 #5
0
        /// <exception cref="System.IO.IOException"/>
        private bool MkdirsWithOptionalPermission(Path f, FsPermission permission)
        {
            if (f == null)
            {
                throw new ArgumentException("mkdirs path arg is null");
            }
            Path     parent   = f.GetParent();
            FilePath p2f      = PathToFile(f);
            FilePath parent2f = null;

            if (parent != null)
            {
                parent2f = PathToFile(parent);
                if (parent2f != null && parent2f.Exists() && !parent2f.IsDirectory())
                {
                    throw new ParentNotDirectoryException("Parent path is not a directory: " + parent
                                                          );
                }
            }
            if (p2f.Exists() && !p2f.IsDirectory())
            {
                throw new FileNotFoundException("Destination exists" + " and is not a directory: "
                                                + p2f.GetCanonicalPath());
            }
            return((parent == null || parent2f.Exists() || Mkdirs(parent)) && (MkOneDirWithMode
                                                                                   (f, p2f, permission) || p2f.IsDirectory()));
        }
예제 #6
0
 /// <exception cref="System.IO.IOException"/>
 public static void TrashNonDefaultFS(Configuration conf)
 {
     conf.SetLong(FsTrashIntervalKey, 10);
     {
         // 10 minute
         // attempt non-default FileSystem trash
         FileSystem lfs = FileSystem.GetLocal(conf);
         Path       p   = TestDir;
         Path       f   = new Path(p, "foo/bar");
         if (lfs.Exists(p))
         {
             lfs.Delete(p, true);
         }
         try
         {
             FileSystemTestHelper.WriteFile(lfs, f, 10);
             FileSystem.CloseAll();
             FileSystem localFs = FileSystem.Get(URI.Create("file:///"), conf);
             Trash      lTrash  = new Trash(localFs, conf);
             lTrash.MoveToTrash(f.GetParent());
             CheckTrash(localFs, lTrash.GetCurrentTrashDir(), f);
         }
         finally
         {
             if (lfs.Exists(p))
             {
                 lfs.Delete(p, true);
             }
         }
     }
 }
예제 #7
0
        /// <summary>Two buffer dirs.</summary>
        /// <remarks>
        /// Two buffer dirs. The first dir does not exist & is on a read-only disk;
        /// The second dir exists & is RW
        /// getLocalPathForWrite with checkAccess set to false should create a parent
        /// directory. With checkAccess true, the directory should not be created.
        /// </remarks>
        /// <exception cref="System.Exception"/>
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestLocalPathForWriteDirCreation()
        {
            string dir0 = BuildBufferDir(Root, 0);
            string dir1 = BuildBufferDir(Root, 1);

            try
            {
                conf.Set(Context, dir0 + "," + dir1);
                Assert.True(localFs.Mkdirs(new Path(dir1)));
                BufferRoot.SetReadOnly();
                Path p1 = dirAllocator.GetLocalPathForWrite("p1/x", SmallFileSize, conf);
                Assert.True(localFs.GetFileStatus(p1.GetParent()).IsDirectory()
                            );
                Path p2 = dirAllocator.GetLocalPathForWrite("p2/x", SmallFileSize, conf, false);
                try
                {
                    localFs.GetFileStatus(p2.GetParent());
                }
                catch (Exception e)
                {
                    Assert.Equal(e.GetType(), typeof(FileNotFoundException));
                }
            }
            finally
            {
                Shell.ExecCommand(Shell.GetSetPermissionCommand("u+w", false, BufferDirRoot));
                RmBufferDirs();
            }
        }
예제 #8
0
        /// <exception cref="System.IO.IOException"/>
        public override void CreateSymlink(Path target, Path link, bool createParent)
        {
            if (!FileSystem.AreSymlinksEnabled())
            {
                throw new NotSupportedException("Symlinks not supported");
            }
            string targetScheme = target.ToUri().GetScheme();

            if (targetScheme != null && !"file".Equals(targetScheme))
            {
                throw new IOException("Unable to create symlink to non-local file " + "system: "
                                      + target.ToString());
            }
            if (createParent)
            {
                Mkdirs(link.GetParent());
            }
            // NB: Use createSymbolicLink in java.nio.file.Path once available
            int result = FileUtil.SymLink(target.ToString(), MakeAbsolute(link).ToString());

            if (result != 0)
            {
                throw new IOException("Error " + result + " creating symlink " + link + " to " +
                                      target);
            }
        }
예제 #9
0
        public virtual void TestCreateDirectory()
        {
            Path path       = QualifiedPath("test/hadoop", fc2);
            Path falsePath  = QualifiedPath("path/doesnot.exist", fc2);
            Path subDirPath = QualifiedPath("dir0", fc2);

            // Ensure that testPath does not exist in fc1
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.Exists(fc1, path));
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.IsFile(fc1, path));
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.IsDir(fc1, path));
            // Create a directory on fc2's file system using fc1
            fc1.Mkdir(path, FsPermission.GetDefault(), true);
            // Ensure fc2 has directory
            Assert.True(FileContextTestHelper.IsDir(fc2, path));
            Assert.True(FileContextTestHelper.Exists(fc2, path));
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.IsFile(fc2, path));
            // Test to create same dir twice, (HDFS mkdir is similar to mkdir -p )
            fc1.Mkdir(subDirPath, FsPermission.GetDefault(), true);
            // This should not throw exception
            fc1.Mkdir(subDirPath, FsPermission.GetDefault(), true);
            // Create Sub Dirs
            fc1.Mkdir(subDirPath, FsPermission.GetDefault(), true);
            // Check parent dir
            Path parentDir = path.GetParent();

            Assert.True(FileContextTestHelper.Exists(fc2, parentDir));
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.IsFile(fc2, parentDir));
            // Check parent parent dir
            Path grandparentDir = parentDir.GetParent();

            Assert.True(FileContextTestHelper.Exists(fc2, grandparentDir));
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.IsFile(fc2, grandparentDir));
            // Negative test cases
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.Exists(fc2, falsePath));
            NUnit.Framework.Assert.IsFalse(FileContextTestHelper.IsDir(fc2, falsePath));
            // TestCase - Create multiple directories
            string[] dirNames = new string[] { "createTest/testDir", "createTest/test Dir", "deleteTest/test*Dir"
                                               , "deleteTest/test#Dir", "deleteTest/test1234", "deleteTest/test_DIr", "deleteTest/1234Test"
                                               , "deleteTest/test)Dir", "deleteTest/()&^%$#@!~_+}{><?", "  ", "^ " };
            foreach (string f in dirNames)
            {
                if (!IsTestableFileNameOnPlatform(f))
                {
                    continue;
                }
                // Create a file on fc2's file system using fc1
                Path testPath = QualifiedPath(f, fc2);
                // Ensure file does not exist
                NUnit.Framework.Assert.IsFalse(FileContextTestHelper.Exists(fc2, testPath));
                // Now create directory
                fc1.Mkdir(testPath, FsPermission.GetDefault(), true);
                // Ensure fc2 has the created directory
                Assert.True(FileContextTestHelper.Exists(fc2, testPath));
                Assert.True(FileContextTestHelper.IsDir(fc2, testPath));
            }
        }
예제 #10
0
        /// <summary>
        /// Return a fully-qualified version of the given symlink target if it
        /// has no scheme and authority.
        /// </summary>
        /// <remarks>
        /// Return a fully-qualified version of the given symlink target if it
        /// has no scheme and authority. Partially and fully-qualified paths
        /// are returned unmodified.
        /// </remarks>
        /// <param name="pathURI">URI of the filesystem of pathWithLink</param>
        /// <param name="pathWithLink">Path that contains the symlink</param>
        /// <param name="target">The symlink's absolute target</param>
        /// <returns>Fully qualified version of the target.</returns>
        public static Path QualifySymlinkTarget(URI pathURI, Path pathWithLink, Path target
                                                )
        {
            // NB: makeQualified uses the target's scheme and authority, if
            // specified, and the scheme and authority of pathURI, if not.
            URI    targetUri = target.ToUri();
            string scheme    = targetUri.GetScheme();
            string auth      = targetUri.GetAuthority();

            return((scheme == null && auth == null) ? target.MakeQualified(pathURI, pathWithLink
                                                                           .GetParent()) : target);
        }
예제 #11
0
            /// <summary>Creates a file on the local FS.</summary>
            /// <remarks>
            /// Creates a file on the local FS. Pass size as
            /// <see cref="LocalDirAllocator.SIZE_UNKNOWN"/>
            /// if not known apriori. We
            /// round-robin over the set of disks (via the configured dirs) and return
            /// a file on the first path which has enough space. The file is guaranteed
            /// to go away when the JVM exits.
            /// </remarks>
            /// <exception cref="System.IO.IOException"/>
            public virtual FilePath CreateTmpFileForWrite(string pathStr, long size, Configuration
                                                          conf)
            {
                // find an appropriate directory
                Path     path   = GetLocalPathForWrite(pathStr, size, conf, true);
                FilePath dir    = new FilePath(path.GetParent().ToUri().GetPath());
                string   prefix = path.GetName();
                // create a temp file on this directory
                FilePath result = FilePath.CreateTempFile(prefix, null, dir);

                result.DeleteOnExit();
                return(result);
            }
예제 #12
0
 public override void Initialize(Configuration conf, FileSystem fs, Path home)
 {
     this.fs               = fs;
     this.trash            = new Path(home, Trash);
     this.homesParent      = home.GetParent();
     this.current          = new Path(trash, Current);
     this.deletionInterval = (long)(conf.GetFloat(CommonConfigurationKeysPublic.FsTrashIntervalKey
                                                  , CommonConfigurationKeysPublic.FsTrashIntervalDefault) * MsecsPerMinute);
     this.emptierInterval = (long)(conf.GetFloat(CommonConfigurationKeysPublic.FsTrashCheckpointIntervalKey
                                                 , CommonConfigurationKeysPublic.FsTrashCheckpointIntervalDefault) * MsecsPerMinute
                                   );
     Log.Info("Namenode trash configuration: Deletion interval = " + (this.deletionInterval
                                                                      / MsecsPerMinute) + " minutes, Emptier interval = " + (this.emptierInterval / MsecsPerMinute
                                                                                                                             ) + " minutes.");
 }
        public virtual void TestEditsLogRename()
        {
            DistributedFileSystem fs = cluster.GetFileSystem();
            Path src1 = GetTestRootPath(fc, "testEditsLogRename/srcdir/src1");
            Path dst1 = GetTestRootPath(fc, "testEditsLogRename/dstdir/dst1");

            CreateFile(src1);
            fs.Mkdirs(dst1.GetParent());
            CreateFile(dst1);
            // Set quota so that dst1 parent cannot allow under it new files/directories
            fs.SetQuota(dst1.GetParent(), 2, HdfsConstants.QuotaDontSet);
            // Free up quota for a subsequent rename
            fs.Delete(dst1, true);
            Rename(src1, dst1, true, true, false, Options.Rename.Overwrite);
            // Restart the cluster and ensure the above operations can be
            // loaded from the edits log
            RestartCluster();
            fs   = cluster.GetFileSystem();
            src1 = GetTestRootPath(fc, "testEditsLogRename/srcdir/src1");
            dst1 = GetTestRootPath(fc, "testEditsLogRename/dstdir/dst1");
            NUnit.Framework.Assert.IsFalse(fs.Exists(src1));
            // ensure src1 is already renamed
            NUnit.Framework.Assert.IsTrue(fs.Exists(dst1));
        }
예제 #14
0
        /// <exception cref="System.IO.IOException"/>
        protected internal virtual string GenerateAndLogErrorListing(Org.Apache.Hadoop.FS.Path
                                                                     src, Org.Apache.Hadoop.FS.Path dst)
        {
            FileSystem fs = GetFileSystem();

            GetLog().Error("src dir " + ContractTestUtils.Ls(fs, src.GetParent()));
            string destDirLS = ContractTestUtils.Ls(fs, dst.GetParent());

            if (fs.IsDirectory(dst))
            {
                //include the dir into the listing
                destDirLS = destDirLS + "\n" + ContractTestUtils.Ls(fs, dst);
            }
            return(destDirLS);
        }
예제 #15
0
        /// <exception cref="System.IO.IOException"/>
        private FSDataOutputStream Create(Path f, bool overwrite, bool createParent, int
                                          bufferSize, short replication, long blockSize, Progressable progress, FsPermission
                                          permission)
        {
            if (Exists(f) && !overwrite)
            {
                throw new FileAlreadyExistsException("File already exists: " + f);
            }
            Path parent = f.GetParent();

            if (parent != null && !Mkdirs(parent))
            {
                throw new IOException("Mkdirs failed to create " + parent.ToString());
            }
            return(new FSDataOutputStream(new BufferedOutputStream(CreateOutputStreamWithMode
                                                                       (f, false, permission), bufferSize), statistics));
        }
예제 #16
0
        /// <exception cref="System.IO.IOException"/>
        private FSDataOutputStream Create(Path f, FsPermission permission, bool overwrite
                                          , bool createParent, int bufferSize, short replication, long blockSize, Progressable
                                          progress)
        {
            Path parent = f.GetParent();

            if (parent != null)
            {
                if (!createParent && !Exists(parent))
                {
                    throw new FileNotFoundException("Parent directory doesn't exist: " + parent);
                }
                else
                {
                    if (!Mkdirs(parent))
                    {
                        throw new IOException("Mkdirs failed to create " + parent + " (exists=" + Exists(
                                                  parent) + ", cwd=" + GetWorkingDirectory() + ")");
                    }
                }
            }
            FSDataOutputStream @out;

            if (writeChecksum)
            {
                @out = new FSDataOutputStream(new ChecksumFileSystem.ChecksumFSOutputSummer(this,
                                                                                            f, overwrite, bufferSize, replication, blockSize, progress, permission), null);
            }
            else
            {
                @out = fs.Create(f, permission, overwrite, bufferSize, replication, blockSize, progress
                                 );
                // remove the checksum file since we aren't writing one
                Path checkFile = GetChecksumFile(f);
                if (fs.Exists(checkFile))
                {
                    fs.Delete(checkFile, true);
                }
            }
            return(@out);
        }
예제 #17
0
            /// <exception cref="System.IO.IOException"/>
            private Path CreatePath(string path, bool checkWrite)
            {
                Path file = new Path(new Path(localDirs[dirNumLastAccessed]), path);

                if (checkWrite)
                {
                    //check whether we are able to create a directory here. If the disk
                    //happens to be RDONLY we will fail
                    try
                    {
                        DiskChecker.CheckDir(new FilePath(file.GetParent().ToUri().GetPath()));
                        return(file);
                    }
                    catch (DiskChecker.DiskErrorException d)
                    {
                        Log.Warn("Disk Error Exception: ", d);
                        return(null);
                    }
                }
                return(file);
            }
예제 #18
0
        /// <summary>
        /// Test trash for the shell's delete command for the default file system
        /// specified in the paramter conf
        /// </summary>
        /// <param name="conf"></param>
        /// <param name="base">- the base path where files are created</param>
        /// <param name="trashRoot">- the expected place where the trashbin resides</param>
        /// <exception cref="System.IO.IOException"/>
        public static void TrashShell(Configuration conf, Path @base, FileSystem trashRootFs
                                      , Path trashRoot)
        {
            FileSystem fs = FileSystem.Get(conf);

            conf.SetLong(FsTrashIntervalKey, 0);
            // disabled
            NUnit.Framework.Assert.IsFalse(new Trash(conf).IsEnabled());
            conf.SetLong(FsTrashIntervalKey, 10);
            // 10 minute
            Assert.True(new Trash(conf).IsEnabled());
            FsShell shell = new FsShell();

            shell.SetConf(conf);
            if (trashRoot == null)
            {
                trashRoot = shell.GetCurrentTrashDir();
            }
            if (trashRootFs == null)
            {
                trashRootFs = fs;
            }
            // First create a new directory with mkdirs
            Path myPath = new Path(@base, "test/mkdirs");

            Mkdir(fs, myPath);
            // Second, create a file in that directory.
            Path myFile = new Path(@base, "test/mkdirs/myFile");

            FileSystemTestHelper.WriteFile(fs, myFile, 10);
            {
                // Verify that expunge without Trash directory
                // won't throw Exception
                string[] args = new string[1];
                args[0] = "-expunge";
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
            }
            {
                // Verify that we succeed in removing the file we created.
                // This should go into Trash.
                string[] args = new string[2];
                args[0] = "-rm";
                args[1] = myFile.ToString();
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
                CheckTrash(trashRootFs, trashRoot, fs.MakeQualified(myFile));
            }
            // Verify that we can recreate the file
            FileSystemTestHelper.WriteFile(fs, myFile, 10);
            {
                // Verify that we succeed in removing the file we re-created
                string[] args = new string[2];
                args[0] = "-rm";
                args[1] = new Path(@base, "test/mkdirs/myFile").ToString();
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
            }
            // Verify that we can recreate the file
            FileSystemTestHelper.WriteFile(fs, myFile, 10);
            {
                // Verify that we succeed in removing the whole directory
                // along with the file inside it.
                string[] args = new string[2];
                args[0] = "-rmr";
                args[1] = new Path(@base, "test/mkdirs").ToString();
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
            }
            // recreate directory
            Mkdir(fs, myPath);
            {
                // Verify that we succeed in removing the whole directory
                string[] args = new string[2];
                args[0] = "-rmr";
                args[1] = new Path(@base, "test/mkdirs").ToString();
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
            }
            {
                // Check that we can delete a file from the trash
                Path toErase = new Path(trashRoot, "toErase");
                int  retVal  = -1;
                FileSystemTestHelper.WriteFile(trashRootFs, toErase, 10);
                try
                {
                    retVal = shell.Run(new string[] { "-rm", toErase.ToString() });
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(retVal == 0);
                CheckNotInTrash(trashRootFs, trashRoot, toErase.ToString());
                CheckNotInTrash(trashRootFs, trashRoot, toErase.ToString() + ".1");
            }
            {
                // simulate Trash removal
                string[] args = new string[1];
                args[0] = "-expunge";
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
            }
            // verify that after expunging the Trash, it really goes away
            CheckNotInTrash(trashRootFs, trashRoot, new Path(@base, "test/mkdirs/myFile").ToString
                                ());
            // recreate directory and file
            Mkdir(fs, myPath);
            FileSystemTestHelper.WriteFile(fs, myFile, 10);
            {
                // remove file first, then remove directory
                string[] args = new string[2];
                args[0] = "-rm";
                args[1] = myFile.ToString();
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
                CheckTrash(trashRootFs, trashRoot, myFile);
                args    = new string[2];
                args[0] = "-rmr";
                args[1] = myPath.ToString();
                val     = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
                CheckTrash(trashRootFs, trashRoot, myPath);
            }
            {
                // attempt to remove parent of trash
                string[] args = new string[2];
                args[0] = "-rmr";
                args[1] = trashRoot.GetParent().GetParent().ToString();
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.Equal("exit code", 1, val);
                Assert.True(trashRootFs.Exists(trashRoot));
            }
            // Verify skip trash option really works
            // recreate directory and file
            Mkdir(fs, myPath);
            FileSystemTestHelper.WriteFile(fs, myFile, 10);
            {
                // Verify that skip trash option really skips the trash for files (rm)
                string[] args = new string[3];
                args[0] = "-rm";
                args[1] = "-skipTrash";
                args[2] = myFile.ToString();
                int val = -1;
                try
                {
                    // Clear out trash
                    Assert.Equal("-expunge failed", 0, shell.Run(new string[] { "-expunge" }));
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                NUnit.Framework.Assert.IsFalse("Expected TrashRoot (" + trashRoot + ") to exist in file system:"
                                               + trashRootFs.GetUri(), trashRootFs.Exists(trashRoot));
                // No new Current should be created
                NUnit.Framework.Assert.IsFalse(fs.Exists(myFile));
                Assert.True(val == 0);
            }
            // recreate directory and file
            Mkdir(fs, myPath);
            FileSystemTestHelper.WriteFile(fs, myFile, 10);
            {
                // Verify that skip trash option really skips the trash for rmr
                string[] args = new string[3];
                args[0] = "-rmr";
                args[1] = "-skipTrash";
                args[2] = myPath.ToString();
                int val = -1;
                try
                {
                    // Clear out trash
                    Assert.Equal(0, shell.Run(new string[] { "-expunge" }));
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                NUnit.Framework.Assert.IsFalse(trashRootFs.Exists(trashRoot));
                // No new Current should be created
                NUnit.Framework.Assert.IsFalse(fs.Exists(myPath));
                NUnit.Framework.Assert.IsFalse(fs.Exists(myFile));
                Assert.True(val == 0);
            }
            {
                // deleting same file multiple times
                int val = -1;
                Mkdir(fs, myPath);
                try
                {
                    Assert.Equal(0, shell.Run(new string[] { "-expunge" }));
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from fs expunge " + e.GetLocalizedMessage
                                                       ());
                }
                // create a file in that directory.
                myFile = new Path(@base, "test/mkdirs/myFile");
                string[] args     = new string[] { "-rm", myFile.ToString() };
                int      num_runs = 10;
                for (int i = 0; i < num_runs; i++)
                {
                    //create file
                    FileSystemTestHelper.WriteFile(fs, myFile, 10);
                    // delete file
                    try
                    {
                        val = shell.Run(args);
                    }
                    catch (Exception e)
                    {
                        System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                           ());
                    }
                    Assert.True(val == 0);
                }
                // current trash directory
                Path trashDir = Path.MergePaths(new Path(trashRoot.ToUri().GetPath()), new Path(myFile
                                                                                                .GetParent().ToUri().GetPath()));
                System.Console.Out.WriteLine("Deleting same myFile: myFile.parent=" + myFile.GetParent
                                                 ().ToUri().GetPath() + "; trashroot=" + trashRoot.ToUri().GetPath() + "; trashDir="
                                             + trashDir.ToUri().GetPath());
                int count = CountSameDeletedFiles(fs, trashDir, myFile);
                System.Console.Out.WriteLine("counted " + count + " files " + myFile.GetName() +
                                             "* in " + trashDir);
                Assert.True(count == num_runs);
            }
            {
                //Verify skipTrash option is suggested when rm fails due to its absence
                string[] args = new string[2];
                args[0] = "-rmr";
                args[1] = "/";
                //This always contains trash directory
                TextWriter            stdout     = System.Console.Out;
                TextWriter            stderr     = System.Console.Error;
                ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
                TextWriter            newOut     = new TextWriter(byteStream);
                Runtime.SetOut(newOut);
                Runtime.SetErr(newOut);
                try
                {
                    shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                string output = byteStream.ToString();
                Runtime.SetOut(stdout);
                Runtime.SetErr(stderr);
                Assert.True("skipTrash wasn't suggested as remedy to failed rm command"
                            + " or we deleted / even though we could not get server defaults", output.IndexOf
                                ("Consider using -skipTrash option") != -1 || output.IndexOf("Failed to determine server trash configuration"
                                                                                             ) != -1);
            }
            {
                // Verify old checkpoint format is recognized
                // emulate two old trash checkpoint directories, one that is old enough
                // to be deleted on the next expunge and one that isn't.
                long       trashInterval       = conf.GetLong(FsTrashIntervalKey, FsTrashIntervalDefault);
                long       now                 = Time.Now();
                DateFormat oldCheckpointFormat = new SimpleDateFormat("yyMMddHHmm");
                Path       dirToDelete         = new Path(trashRoot.GetParent(), oldCheckpointFormat.Format(now
                                                                                                            - (trashInterval * 60 * 1000) - 1));
                Path dirToKeep = new Path(trashRoot.GetParent(), oldCheckpointFormat.Format(now));
                Mkdir(trashRootFs, dirToDelete);
                Mkdir(trashRootFs, dirToKeep);
                // Clear out trash
                int rc = -1;
                try
                {
                    rc = shell.Run(new string[] { "-expunge" });
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from fs expunge " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.Equal(0, rc);
                NUnit.Framework.Assert.IsFalse("old checkpoint format not recognized", trashRootFs
                                               .Exists(dirToDelete));
                Assert.True("old checkpoint format directory should not be removed"
                            , trashRootFs.Exists(dirToKeep));
            }
        }
예제 #19
0
 /// <summary>Return the name of the checksum file associated with a file.</summary>
 public virtual Path GetChecksumFile(Path file)
 {
     return(new Path(file.GetParent(), "." + file.GetName() + ".crc"));
 }
예제 #20
0
        /// <exception cref="System.Exception"/>
        public virtual void TestTrashEmptier()
        {
            Configuration conf = new Configuration();

            // Trash with 12 second deletes and 6 seconds checkpoints
            conf.Set(FsTrashIntervalKey, "0.2");
            // 12 seconds
            conf.SetClass("fs.file.impl", typeof(TestTrash.TestLFS), typeof(FileSystem));
            conf.Set(FsTrashCheckpointIntervalKey, "0.1");
            // 6 seconds
            FileSystem fs = FileSystem.GetLocal(conf);

            conf.Set("fs.default.name", fs.GetUri().ToString());
            Trash trash = new Trash(conf);
            // Start Emptier in background
            Runnable emptier       = trash.GetEmptier();
            Thread   emptierThread = new Thread(emptier);

            emptierThread.Start();
            FsShell shell = new FsShell();

            shell.SetConf(conf);
            shell.Init();
            // First create a new directory with mkdirs
            Path myPath = new Path(TestDir, "test/mkdirs");

            Mkdir(fs, myPath);
            int fileIndex = 0;
            ICollection <string> checkpoints = new HashSet <string>();

            while (true)
            {
                // Create a file with a new name
                Path myFile = new Path(TestDir, "test/mkdirs/myFile" + fileIndex++);
                FileSystemTestHelper.WriteFile(fs, myFile, 10);
                // Delete the file to trash
                string[] args = new string[2];
                args[0] = "-rm";
                args[1] = myFile.ToString();
                int val = -1;
                try
                {
                    val = shell.Run(args);
                }
                catch (Exception e)
                {
                    System.Console.Error.WriteLine("Exception raised from Trash.run " + e.GetLocalizedMessage
                                                       ());
                }
                Assert.True(val == 0);
                Path         trashDir = shell.GetCurrentTrashDir();
                FileStatus[] files    = fs.ListStatus(trashDir.GetParent());
                // Scan files in .Trash and add them to set of checkpoints
                foreach (FileStatus file in files)
                {
                    string fileName = file.GetPath().GetName();
                    checkpoints.AddItem(fileName);
                }
                // If checkpoints has 4 objects it is Current + 3 checkpoint directories
                if (checkpoints.Count == 4)
                {
                    // The actual contents should be smaller since the last checkpoint
                    // should've been deleted and Current might not have been recreated yet
                    Assert.True(checkpoints.Count > files.Length);
                    break;
                }
                Thread.Sleep(5000);
            }
            emptierThread.Interrupt();
            emptierThread.Join();
        }