Skip to content

Commit

Permalink
Added test to reproduce problem
Browse files Browse the repository at this point in the history
  • Loading branch information
picrap committed Sep 12, 2017
1 parent 6026875 commit 337de9e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 8 deletions.
30 changes: 22 additions & 8 deletions ExFat.Core/Filesystem/ExFatPathFilesystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,21 @@ public void Dispose()
_entryFilesystem.Dispose();
}

private ExFatFilesystemEntry GetEntry(string path)
private ExFatFilesystemEntry GetEntry(string cleanPath)
{
if (path == null)
if (cleanPath == null)
throw new ArgumentNullException();
path = CleanupPath(path);
if (path == "")
if (cleanPath == "")
return _entryFilesystem.RootDirectory;

lock (_entriesLock)
{
if (_entries.TryGetValue(path, out var entry))
if (_entries.TryGetValue(cleanPath, out var entry))
return entry;

var pn = GetParentAndName(path);
var pn = GetParentAndName(cleanPath);
entry = GetEntry(pn.Item1, pn.Item2);
return Register(entry, path);
return Register(entry, cleanPath);
}
}

Expand All @@ -117,6 +116,12 @@ private ExFatFilesystemEntry Register(ExFatFilesystemEntry entry, string cleanPa
return entry;
}

private void Unregister(string cleanPath)
{
lock (_entriesLock)
_entries.Remove(cleanPath);
}

private Tuple<string, string> GetParentAndName(string cleanPath)
{
var separatorIndex = cleanPath.LastIndexOfAny(_separators);
Expand All @@ -141,6 +146,11 @@ private ExFatFilesystemEntry GetEntry(string cleanParentPath, string childName)
private string GetPath(string cleanParentPath, ExFatFilesystemEntry entry)
{
var fileName = entry.MetaEntry.ExtensionsFileName;
return GetPath(cleanParentPath, fileName);
}

private string GetPath(string cleanParentPath, string fileName)
{
// on root entries, the direct name is returned
if (cleanParentPath == "")
return fileName;
Expand Down Expand Up @@ -378,8 +388,9 @@ public void Move(string sourcePath, string targetDirectory, string targetName =
throw new ArgumentNullException(nameof(targetDirectory), "Either targetDirectory or targetName has to be provided");

var cleanSourcePath = CleanupPath(sourcePath);
var cleanSourceParentAndName = GetParentAndName(cleanSourcePath);
if (targetDirectory == null)
targetDirectory = GetParentAndName(cleanSourcePath).Item1;
targetDirectory = cleanSourceParentAndName.Item1;
var cleanTargetDirectory = CleanupPath(targetDirectory);

var sourceEntry = GetEntry(cleanSourcePath);
Expand All @@ -389,7 +400,10 @@ public void Move(string sourcePath, string targetDirectory, string targetName =
if (targetDirectoryEntry == null)
throw new FileNotFoundException();

var cleanTargetPath = cleanTargetDirectory;
cleanTargetPath = GetPath(cleanTargetPath, targetName ?? cleanSourceParentAndName.Item2);
_entryFilesystem.Move(sourceEntry, targetDirectoryEntry, targetName);
Unregister(cleanTargetPath);
Register(null, cleanSourcePath);
}

Expand Down
37 changes: 37 additions & 0 deletions ExFat.DiscUtils.Tests/Tests/DiscFilesystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,42 @@ public void ReadRootDirectories()
}
}
}

[TestMethod]
[TestCategory("Write")]
public void MoveFile()
{
using (var testEnvironment = new TestEnvironment())
{
using (var filesystem = new ExFatFileSystem(testEnvironment.PartitionStream))
{
using (var a = filesystem.OpenFile("a", FileMode.Create))
a.WriteByte(1);
Assert.IsTrue(filesystem.FileExists("a"));
filesystem.MoveFile("a", "b");
Assert.IsFalse(filesystem.FileExists("a"));
Assert.IsTrue(filesystem.FileExists("b"));
}
}
}

[TestMethod]
[TestCategory("Write")]
public void MoveFileToDirectory()
{
using (var testEnvironment = new TestEnvironment())
{
using (var filesystem = new ExFatFileSystem(testEnvironment.PartitionStream))
{
using (var a = filesystem.OpenFile("a", FileMode.Create))
a.WriteByte(1);
filesystem.CreateDirectory("d");
Assert.IsTrue(filesystem.FileExists("a"));
filesystem.MoveFile("a", "d");
Assert.IsFalse(filesystem.FileExists("a"));
Assert.IsTrue(filesystem.FileExists("d\\a"));
}
}
}
}
}
15 changes: 15 additions & 0 deletions ExFat.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ namespace ExFat.Generator
public static class Program
{
public static void Main(string[] args)
{
using (var diskStream = File.OpenRead("D:\\rozina-pascal.localcopy.vhdx"))
{
var disk = new Disk(diskStream, Ownership.Dispose);
var volume = VolumeManager.GetPhysicalVolumes(disk).First();
var volumeStream = volume.Open();
using (var fs = new ExFatFileSystem(volumeStream))
{
var f = fs.FileExists(@"rozina-pascal\storage\parameters");
var d = fs.DirectoryExists(@"rozina-pascal\storage\parameters");
}
}
}

public static void Main4(string[] args)
{
string label = "Zap!";
long capacity = 2L << 40;
Expand Down

0 comments on commit 337de9e

Please sign in to comment.