Exemplo n.º 1
0
 /// <summary>The src file is under FS, and the dst is on the local disk.</summary>
 /// <remarks>
 /// The src file is under FS, and the dst is on the local disk.
 /// Copy it from FS control to the local dst name.
 /// If src and dst are directories, the copyCrc parameter
 /// determines whether to copy CRC files.
 /// </remarks>
 /// <exception cref="System.IO.IOException"/>
 public virtual void CopyToLocalFile(Path src, Path dst, bool copyCrc)
 {
     if (!fs.IsDirectory(src))
     {
         // source is a file
         fs.CopyToLocalFile(src, dst);
         FileSystem localFs = GetLocal(GetConf()).GetRawFileSystem();
         if (localFs.IsDirectory(dst))
         {
             dst = new Path(dst, src.GetName());
         }
         dst = GetChecksumFile(dst);
         if (localFs.Exists(dst))
         {
             //remove old local checksum file
             localFs.Delete(dst, true);
         }
         Path checksumFile = GetChecksumFile(src);
         if (copyCrc && fs.Exists(checksumFile))
         {
             //copy checksum file
             fs.CopyToLocalFile(checksumFile, dst);
         }
     }
     else
     {
         FileStatus[] srcs = ListStatus(src);
         foreach (FileStatus srcFile in srcs)
         {
             CopyToLocalFile(srcFile.GetPath(), new Path(dst, srcFile.GetPath().GetName()), copyCrc
                             );
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>Rename files/dirs</summary>
 /// <exception cref="System.IO.IOException"/>
 public override bool Rename(Path src, Path dst)
 {
     if (fs.IsDirectory(src))
     {
         return(fs.Rename(src, dst));
     }
     else
     {
         if (fs.IsDirectory(dst))
         {
             dst = new Path(dst, src.GetName());
         }
         bool value = fs.Rename(src, dst);
         if (!value)
         {
             return(false);
         }
         Path srcCheckFile = GetChecksumFile(src);
         Path dstCheckFile = GetChecksumFile(dst);
         if (fs.Exists(srcCheckFile))
         {
             //try to rename checksum
             value = fs.Rename(srcCheckFile, dstCheckFile);
         }
         else
         {
             if (fs.Exists(dstCheckFile))
             {
                 // no src checksum, so remove dst checksum
                 value = fs.Delete(dstCheckFile, true);
             }
         }
         return(value);
     }
 }
Exemplo n.º 3
0
 public virtual bool Accept(Path path)
 {
     return(pattern.Matches(path.GetName()) && userFilter.Accept(path));
 }
Exemplo n.º 4
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));
            }
        }
Exemplo n.º 5
0
 public bool Accept(Path file)
 {
     return(file.GetName().StartsWith(prefix));
 }
Exemplo n.º 6
0
        /// <summary>Return true iff file is a checksum file name.</summary>
        public static bool IsChecksumFile(Path file)
        {
            string name = file.GetName();

            return(name.StartsWith(".") && name.EndsWith(".crc"));
        }
Exemplo n.º 7
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"));
 }