private void ImportLocalDirectory(List <LocalDirectoryContentInfo> localDirectoryContentInfos, DirectoryInfo directoryInfo, int parentDirectoryCluster) { int newDirectoryClusterIndex = AddNextFreeClusterToChain(); _clusterInfos[newDirectoryClusterIndex] = new ClusterInfo() { FileOffset = -1, DataBuffer = new byte[Parameters.BytesPerCluster] }; FatAddDirectoryEntry(localDirectoryContentInfos, parentDirectoryCluster, newDirectoryClusterIndex, directoryInfo.Parent.FullName, directoryInfo.Name, FAT16Helper.GetShortFileName(directoryInfo.Name), FAT16Helper.DirectoryIdentifier, directoryInfo.LastWriteTime, 0); FatAddDirectoryEntry(localDirectoryContentInfos, newDirectoryClusterIndex, newDirectoryClusterIndex, directoryInfo.FullName, ".", ".", FAT16Helper.DirectoryIdentifier, directoryInfo.LastWriteTime, 0); FatAddDirectoryEntry(localDirectoryContentInfos, newDirectoryClusterIndex, parentDirectoryCluster, directoryInfo.Parent.FullName, "..", "..", FAT16Helper.DirectoryIdentifier, directoryInfo.LastWriteTime, 0); ImportLocalDiskContent(localDirectoryContentInfos, directoryInfo.FullName, newDirectoryClusterIndex); }
private void ImportLocalFile(List <LocalDirectoryContentInfo> localDirectoryContentInfos, FileInfo fileInfo, int directoryClusterIndex) { long fileOffset = 0; int fileentryStartClusterIndex = 0; int? nextFileClusterIndex = null; while (fileOffset < fileInfo.Length) { try { nextFileClusterIndex = AddNextFreeClusterToChain(nextFileClusterIndex); if (fileentryStartClusterIndex == _rootDirectoryClusterIndex) { fileentryStartClusterIndex = nextFileClusterIndex.Value; } _clusterInfos[nextFileClusterIndex.Value] = new ClusterInfo() { FileOffset = fileOffset }; fileOffset += Parameters.BytesPerCluster; } catch (IndexOutOfRangeException outOfRangeEx) { int localDirectorySizeMiB = (int)Directory.GetFiles(Parameters.LocalDirectoryPath, "*", SearchOption.AllDirectories).Sum(file => (new FileInfo(file).Length)) / FAT16Helper.BytesPerMiB; _logger.LogException(outOfRangeEx, $"Local directory size is {localDirectorySizeMiB} MiB, which is too large for the given virtual disk size ({Parameters.DiskTotalBytes / FAT16Helper.BytesPerMiB} MiB)"); throw; } } // handle duplicate short filenames string TOSFileName = FAT16Helper.GetShortFileName(fileInfo.Name); int duplicateId = 1; while (localDirectoryContentInfos.Where(ldi => ldi.TOSFileName.Equals(TOSFileName, StringComparison.InvariantCultureIgnoreCase) && ldi.DirectoryCluster == directoryClusterIndex).Any()) { int numberStringLength = duplicateId.ToString().Length + 1; // +1 for ~ int replaceIndex = TOSFileName.LastIndexOf('.') != -1 ? TOSFileName.LastIndexOf('.') : TOSFileName.Length; replaceIndex -= numberStringLength; TOSFileName = TOSFileName.Remove(replaceIndex, numberStringLength).Insert(replaceIndex, $"~{duplicateId}"); duplicateId++; } FatAddDirectoryEntry(localDirectoryContentInfos, directoryClusterIndex, fileentryStartClusterIndex, fileInfo.DirectoryName, fileInfo.Name, TOSFileName, 0x00, fileInfo.LastWriteTime, fileInfo.Length); }
public void CreateShortFileNameFromInvalidFileName(string invalidFileName, string expectedShortFileName) { var shortFileName = FAT16Helper.GetShortFileName(invalidFileName); Assert.AreEqual(expectedShortFileName, shortFileName); }
public void CreateShortFileNameFromLongFileName(string longFileName, string expectedShortFileName) { var shortFileName = FAT16Helper.GetShortFileName(longFileName); Assert.AreEqual(expectedShortFileName, shortFileName); }