Exemple #1
0
        private void TestIntersectingHardLinkAndReplace(bool isLongPath, FileShare fileShare)
        {
            var folderPath = this.testPath;

            if (true == isLongPath)
            {
                folderPath = this.ExtendPath(folderPath);
            }

            var filePath = Path.Combine(folderPath, this.testFileName);

            Assert.IsTrue(!isLongPath || filePath.Length > 260, "file path must be greater than max path size.");

            var hardLinkedFilePath = Path.Combine(folderPath, this.testHardLinkedFileName);

            Assert.IsTrue(!isLongPath || hardLinkedFilePath.Length > 260, "hard linked file path must be greater than max path size.");

            var replaceFilePath = Path.Combine(folderPath, this.testReplaceFileName);

            Assert.IsTrue(!isLongPath || replaceFilePath.Length > 260, "replace file path must be greater than max path size.");

            var backupFilePath = Path.Combine(folderPath, this.testBackupFileName);

            Assert.IsTrue(!isLongPath || backupFilePath.Length > 260, "backup file path must be greater than max path size.");

            LogHelper.Log("FabricDirectory.Create {0}", folderPath);
            FabricDirectory.CreateDirectory(folderPath);

            LogHelper.Log("FabricFile.Create {0}", filePath);
            using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(filePath)))
            {
                LogHelper.Log("Write {0}", this.testString);
                streamWriter.WriteLine(this.testString);
                streamWriter.Flush();
            }

            using (StreamWriter streamWriter = new StreamWriter(FabricFile.Create(replaceFilePath)))
            {
                LogHelper.Log("Write {0}", this.testNewString);
                streamWriter.WriteLine(this.testNewString);
                streamWriter.Flush();
            }

            FabricFile.CreateHardLink(hardLinkedFilePath, filePath);

            using (StreamReader hardLinkReader0 = new StreamReader(FabricFile.Open(hardLinkedFilePath, FileMode.Open, FileAccess.Read, fileShare)))
            {
                var hardLinkContent0 = hardLinkReader0.ReadLine();
                Assert.AreEqual <string>(this.testString, hardLinkContent0, "after replace hard link file must have the old content.");

                try
                {
                    FabricFile.Replace(replaceFilePath, filePath, backupFilePath, false);
                    Assert.Fail();
                }
                catch (FileLoadException)
                {
                }
            }
        }
Exemple #2
0
        internal static void CreateHardLinkOrCopyFile(string sourceFileName, string destinationFileName)
        {
            if (!FabricFile.Exists(sourceFileName))
            {
                return;
            }

            // Since the source file won't be changed, we can simply create a hardlink
            if (!FabricFile.CreateHardLink(destinationFileName, sourceFileName))
            {
                // Hardlink can fail, e.g. if the destination is on another drive or the drive isn't NTFS.
                // Fallback to a full file copy.
                FabricFile.Copy(sourceFileName, destinationFileName, overwrite: true);
            }
        }