Browse Source

patch: Get REAL file length for symlink

HOME 3 years ago
parent
commit
a20dcc2fb1

+ 1 - 0
DiskAccessLibrary/DiskAccessLibrary.csproj

@@ -70,6 +70,7 @@
     <Compile Include="Exceptions\DirectoryNotEmptyException.cs" />
     <Compile Include="Exceptions\DiskFullException.cs" />
     <Compile Include="Exceptions\SharingViolationException.cs" />
+    <Compile Include="IoUtility.cs" />
     <Compile Include="FileSystems\FileSystemHelper.cs" />
     <Compile Include="FileSystems\IExtendableFileSystem.cs" />
     <Compile Include="FileSystems\NTFS\Adapters\NTFSFileStream.cs" />

+ 15 - 0
DiskAccessLibrary/IoUtility.cs

@@ -0,0 +1,15 @@
+using System.IO;
+
+namespace DiskAccessLibrary
+{
+    public static class IoUtility
+    {
+        public static long GetRealFileSize(string path)
+        {
+            using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
+            {
+                return fs.Length;
+            }
+        }
+    }
+}

+ 2 - 2
DiskAccessLibrary/Mod/BlockDifferencingDiskImage/BddInfo.cs

@@ -44,8 +44,8 @@ namespace DiskAccessLibrary
             if (null != BasedImagePath)
             {
                 if (false == File.Exists(BasedImagePath)) throw new FileNotFoundException("based image not found", BasedImagePath);
-                using var ms = new FileStream(BasedImagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
-                if (snapMatchLength != ms.Length) throw new InvalidDataException("Snapshot size no match to based image");
+                var bfl=IoUtility.GetRealFileSize(BasedImagePath);
+                if (snapMatchLength != bfl) throw new InvalidDataException("Snapshot size no match to based image");
             }
 
             EntryTableOffset = fs.Position;

+ 4 - 3
DiskAccessLibrary/Mod/BlockDifferencingDiskImage/BlockDifferencingDiskImage.cs

@@ -3,6 +3,7 @@ using System;
 using System.IO;
 using System.Security.Authentication;
 using System.Text;
+using DiskAccessLibrary.FileSystems;
 
 namespace DiskAccessLibrary
 {
@@ -252,9 +253,9 @@ namespace DiskAccessLibrary
 
         public static BlockDifferencingDiskImage Create(string based, string snapshot, int blockSize)
         {
-            var fileInfo = new FileInfo(based);
+            var fileSize= IoUtility.GetRealFileSize(based);
 
-            if (fileInfo.Length % blockSize != 0) throw new ArgumentException("Block size no mul to base image size", nameof(blockSize));
+            if (fileSize % blockSize != 0) throw new ArgumentException("Block size no mul to base image size", nameof(blockSize));
 
             using var stream = File.Create(snapshot);
             using var writer = new BinaryWriter(stream);
@@ -262,7 +263,7 @@ namespace DiskAccessLibrary
             //create bdd struct
 
             var bufBasedPath = Encoding.UTF8.GetBytes(System.IO.Path.GetFullPath(based));
-            var numberOfBlocks = (int)(fileInfo.Length / blockSize);
+            var numberOfBlocks = (int)(fileSize / blockSize);
             writer.Write((ushort)bufBasedPath.Length);
             writer.Write(bufBasedPath);
             writer.Write(blockSize);

+ 1 - 3
DiskAccessLibrary/Mod/DiskImage.cs

@@ -46,9 +46,7 @@ namespace DiskAccessLibrary
                                 break;
                             }
 
-                            long baseLength;
-                            using (var fs = new FileStream(bdd.BasedImagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
-                                baseLength = fs.Length;
+                            var baseLength = IoUtility.GetRealFileSize(bdd.BasedImagePath);
 
                             info.AppendLine($"Based on:    {bdd.BasedImagePath}");
                             info.AppendLine($"Based Size:  {baseLength / MegaByte:N0} MB");

+ 3 - 2
ISCSIConsole/CreateDiskImageForm.cs

@@ -128,9 +128,10 @@ namespace ISCSIConsole
                 ImageFileTextBox.Text = _imageFileDialog.FileName;
                 if ((DiskImageFormat)FormatComboBox.SelectedValue == DiskImageFormat.BlockDifferencingDiskImage)
                 {
-                    var info = new FileInfo(_imageFileDialog.FileName);
+                    var length=IoUtility.GetRealFileSize(_imageFileDialog.FileName);
+
                     // ReSharper disable once PossibleLossOfFraction
-                    ImageSizeUpDown.Value = info.Length / MegaByte;
+                    ImageSizeUpDown.Value = length / MegaByte;
 
                     BlockSizeUpDown_SelectedItemChanged(null, null);
                 }