示例#1
0
        public void TestEnumerateWithOutSelf()
        {
            // Arrange
            const string filenameExisiting = "original.txt";
            const string filename          = "hardlink.txt";

            using (var writer = new StreamWriter(filenameExisiting, false, Encoding.Default))
            {
                writer.WriteLine("test file for hardlink test");
            }

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            HardLink.Create(filename, filenameExisiting);

            // Act
            var files = HardLink.Enumerate(filenameExisiting, false)
                        .Select(Path.GetFileName)
                        .ToArray();

            // Assert
            Assert.AreEqual(files.Length, 1, "number of files not correct");
            Assert.IsTrue(files.Contains(filename), "target file missing");
        }
示例#2
0
        /// <summary>
        /// Remove a hard link by copying the block to a temporary place and
        /// then moving it back
        /// </summary>
        /// <param name="numLinks">number of hard links</param>
        /// <returns>
        /// true if copy is successful;
        /// false if it is already detached or no need to be detached
        /// </returns>
        /// <exception cref="System.IO.IOException">if there is any copy error</exception>
        public virtual bool UnlinkBlock(int numLinks)
        {
            if (IsUnlinked())
            {
                return(false);
            }
            FilePath file = GetBlockFile();

            if (file == null || GetVolume() == null)
            {
                throw new IOException("detachBlock:Block not found. " + this);
            }
            FilePath meta = GetMetaFile();

            if (HardLink.GetLinkCount(file) > numLinks)
            {
                DataNode.Log.Info("CopyOnWrite for block " + this);
                UnlinkFile(file, this);
            }
            if (HardLink.GetLinkCount(meta) > numLinks)
            {
                UnlinkFile(meta, this);
            }
            SetUnlinked();
            return(true);
        }
示例#3
0
        public void TestFileContent()
        {
            // Arrange
            const string filenameExisiting = "original.txt";
            const string filename          = "hardlink.txt";

            const string content = "test file for hardlink test";

            using (var writer = new StreamWriter(filenameExisiting, false, Encoding.Default))
            {
                writer.WriteLine(content);
            }

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            // Act
            HardLink.Create(filename, filenameExisiting);

            string text;

            using (var reader = new StreamReader(filename, Encoding.Default))
            {
                text = reader.ReadLine();
            }

            // Assert
            Assert.AreEqual(content, text, "file content differs");
        }
示例#4
0
        private static void SortImage(string image, string outPath)
        {
            Logger.Log($"Sorting {image}");
            var tags     = image.GetTags();
            var fileName = Path.GetFileName(image);

            if (tags.In == null || tags.Style == null)
            {
                File.Delete(image);
                return;
            }

            var inFile    = Path.Combine(outPath, "name", tags.In, fileName);
            var styleFile = Path.Combine(outPath, "style", tags.Style, fileName);

            inFile.EnsureDirectoryExists();
            styleFile.EnsureDirectoryExists();

            if (File.Exists(inFile))
            {
                File.Delete(inFile);
            }
            File.Move(image, inFile);

            HardLink.Create(inFile, styleFile, true);
        }
示例#5
0
 /// <exception cref="System.IO.IOException"/>
 public static void Link(FilePath src, FilePath dst)
 {
     if (!nativeLoaded)
     {
         HardLink.CreateHardLink(src, dst);
     }
     else
     {
         Link0(src.GetAbsolutePath(), dst.GetAbsolutePath());
     }
 }
示例#6
0
		private string PrepareHardLink(object testClass, [CallerMemberName] string testMethod = null) {
			var testFolder = Helper.GetTestFolder(TestDrive.Root, "WriteTests", testClass, testMethod);
			_fixture = new Fixture(() => Directory.Delete(testFolder, true));

			Directory.CreateDirectory(testFolder);
			var originalFile = Path.Combine(testFolder, "Original.txt");
			File.WriteAllText(originalFile, "Original");
			var hardLink = Path.Combine(testFolder, "HardLink.txt");
			HardLink.Create(hardLink, originalFile);
			Assert.IsTrue(File.Exists(hardLink));
			return hardLink;
		}
示例#7
0
		private void CreateUncheckedTest(int length,[CallerMemberName] string callerName=null) {
			Helper.DemandTestDriveAvailable(TestDrive.Root);
			//Helper.DemandElevated();

			var testFolder = TestDrive.WriteTests.CreateTestFolder(this, callerName);
			_fixture = new Fixture(() => Directory.Delete(testFolder, true));

			var longPathFile = Path.Combine(testFolder, new string('!', length - testFolder.Length -1 - 4))+".txt";
			Assert.AreEqual(length,longPathFile.Length);
			var originalFile = TestDrive.ReadTests.OriginalTxt.FullName;

			HardLink.Create(longPathFile, originalFile);
			Assert.AreEqual(TestDrive.ReadTests.OriginalTxt.Content, File.ReadAllText(longPathFile));
		}
        /// <summary>Hardlink all finalized and RBW blocks in fromDir to toDir</summary>
        /// <param name="fromDir">directory where the snapshot is stored</param>
        /// <param name="toDir">the current data directory</param>
        /// <exception cref="System.IO.IOException">if error occurs during hardlink</exception>
        private void LinkAllBlocks(DataNode datanode, FilePath fromDir, FilePath toDir)
        {
            // do the link
            int diskLayoutVersion = this.GetLayoutVersion();
            // hardlink finalized blocks in tmpDir
            HardLink hardLink = new HardLink();

            DataStorage.LinkBlocks(datanode, new FilePath(fromDir, DataStorage.StorageDirFinalized
                                                          ), new FilePath(toDir, DataStorage.StorageDirFinalized), diskLayoutVersion, hardLink
                                   );
            DataStorage.LinkBlocks(datanode, new FilePath(fromDir, DataStorage.StorageDirRbw)
                                   , new FilePath(toDir, DataStorage.StorageDirRbw), diskLayoutVersion, hardLink);
            Log.Info(hardLink.linkStats.Report());
        }
示例#9
0
		public void CreateLongTargetTest() {
			Helper.DemandTestDriveAvailable(TestDrive.Root);
			//Helper.DemandElevated();

			var testFolder = TestDrive.WriteTests.CreateTestFolder(this);
			_fixture = new Fixture(() => Directory.Delete(testFolder, true));

			var originalFile = TestDrive.ReadTests.LongPath.OriginalTxt.FullName;
			File.WriteAllText(originalFile, "Original");

			var hardLink = Path.Combine(testFolder, "HardLink.txt");
			HardLink.Create(hardLink, originalFile);

			Assert.AreEqual(TestDrive.ReadTests.LongPath.OriginalTxt.Content, File.ReadAllText(hardLink));
		}
示例#10
0
        public void TestSourceException()
        {
            // Arrange
            const string filenameExisiting = "original.txt";
            const string filename          = "hardlink.txt";

            if (File.Exists(filenameExisiting))
            {
                File.Delete(filenameExisiting);
            }

            // Act
            HardLink.Create(filename, filenameExisiting);

            // Assert
        }
示例#11
0
        private static void CreateMissingHardlink(string outPath, string image)
        {
            var fileName = Path.GetFileName(image);
            var tags     = image.GetTags();

            if (tags.Style == null)
            {
                return;
            }

            var styleFile = Path.Combine(outPath, "style", tags.Style, fileName);

            styleFile.EnsureDirectoryExists();

            HardLink.Create(image, styleFile, true);
        }
示例#12
0
		public void CreateTest() {
			Helper.DemandTestDriveAvailable(TestDrive.Root);
			//Helper.DemandElevated();

			var testFolder = Helper.GetTestFolder(TestDrive.Root, "WriteTests", this);
			_fixture =new Fixture(()=>Directory.Delete(testFolder,true));

			Directory.CreateDirectory(testFolder);
			var originalFile = Path.Combine(testFolder, "original.txt");
			File.WriteAllText(originalFile,"Original");

			var hardLink = Path.Combine(testFolder, "hardlink.txt");
			var c0 = HardLink.GetNumberOfLinks(originalFile);
			HardLink.Create(hardLink, originalFile);
			var c1 = HardLink.GetNumberOfLinks(hardLink);

			Assert.AreEqual("Original", File.ReadAllText(hardLink));
			Assert.AreEqual(c0+1,c1);
		}
示例#13
0
        public void TestTargetException()
        {
            // Arrange
            const string filenameExisiting = "original.txt";
            const string filename          = "hardlink.txt";

            using (var writer = new StreamWriter(filenameExisiting, false, Encoding.Default))
            {
                writer.WriteLine("test file for hardlink test");
            }
            using (var writer = new StreamWriter(filename, false, Encoding.Default))
            {
                writer.WriteLine("test file for blocking hardlink");
            }

            // Act
            HardLink.Create(filename, filenameExisiting);

            // Assert
        }
示例#14
0
        public void TestCreate()
        {
            // Arrange
            const string filenameExisiting = "original.txt";
            const string filename          = "hardlink.txt";

            using (var writer = new StreamWriter(filenameExisiting, false, Encoding.Default))
            {
                writer.WriteLine("test file for hardlink test");
            }

            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            // Act
            HardLink.Create(filename, filenameExisiting);

            // Assert
            Assert.IsTrue(File.Exists(filename), "hardlink not created");
        }
示例#15
0
        public virtual void TestCopyOnWrite()
        {
            Configuration  conf    = new HdfsConfiguration();
            MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).Build();
            FileSystem     fs      = cluster.GetFileSystem();
            IPEndPoint     addr    = new IPEndPoint("localhost", cluster.GetNameNodePort());
            DFSClient      client  = new DFSClient(addr, conf);

            try
            {
                // create a new file, write to it and close it.
                //
                Path file1             = new Path("/filestatus.dat");
                FSDataOutputStream stm = AppendTestUtil.CreateFile(fs, file1, 1);
                WriteFile(stm);
                stm.Close();
                // Get a handle to the datanode
                DataNode[] dn = cluster.ListDataNodes();
                NUnit.Framework.Assert.IsTrue("There should be only one datanode but found " + dn
                                              .Length, dn.Length == 1);
                LocatedBlocks locations = client.GetNamenode().GetBlockLocations(file1.ToString()
                                                                                 , 0, long.MaxValue);
                IList <LocatedBlock> blocks = locations.GetLocatedBlocks();
                //
                // Create hard links for a few of the blocks
                //
                for (int i = 0; i < blocks.Count; i = i + 2)
                {
                    ExtendedBlock b = blocks[i].GetBlock();
                    FilePath      f = DataNodeTestUtils.GetFile(dn[0], b.GetBlockPoolId(), b.GetLocalBlock
                                                                    ().GetBlockId());
                    FilePath link = new FilePath(f.ToString() + ".link");
                    System.Console.Out.WriteLine("Creating hardlink for File " + f + " to " + link);
                    HardLink.CreateHardLink(f, link);
                }
                //
                // Detach all blocks. This should remove hardlinks (if any)
                //
                for (int i_1 = 0; i_1 < blocks.Count; i_1++)
                {
                    ExtendedBlock b = blocks[i_1].GetBlock();
                    System.Console.Out.WriteLine("testCopyOnWrite detaching block " + b);
                    NUnit.Framework.Assert.IsTrue("Detaching block " + b + " should have returned true"
                                                  , DataNodeTestUtils.UnlinkBlock(dn[0], b, 1));
                }
                // Since the blocks were already detached earlier, these calls should
                // return false
                //
                for (int i_2 = 0; i_2 < blocks.Count; i_2++)
                {
                    ExtendedBlock b = blocks[i_2].GetBlock();
                    System.Console.Out.WriteLine("testCopyOnWrite detaching block " + b);
                    NUnit.Framework.Assert.IsTrue("Detaching block " + b + " should have returned false"
                                                  , !DataNodeTestUtils.UnlinkBlock(dn[0], b, 1));
                }
            }
            finally
            {
                client.Close();
                fs.Close();
                cluster.Shutdown();
            }
        }