public static DirectoryEntry GetFile(string aPath) { if (string.IsNullOrEmpty(aPath)) { throw new ArgumentNullException("aPath"); } FileSystemHelpers.Debug("VFSManager.GetFile", "aPath = ", aPath); string xFileName = Path.GetFileName(aPath); string xDirectory = Path.GetDirectoryName(aPath); char xLastChar = xDirectory[xDirectory.Length - 1]; if (xLastChar != Path.DirectorySeparatorChar) { xDirectory = xDirectory + Path.DirectorySeparatorChar; } var xList = GetDirectoryListing(xDirectory); for (int i = 0; i < xList.Count; i++) { var xEntry = xList[i]; if ((xEntry != null) && (xEntry.mEntryType == DirectoryEntryTypeEnum.File) && (xEntry.mName.ToUpper() == xFileName.ToUpper())) { return(xEntry); } } return(null); }
private void WriteFatTableSector(ulong xSectorNum, byte[] aData) { FileSystemHelpers.Debug("Fat.WriteFatTableSector", "xSectorNum =", xSectorNum, ", aData.Length = ", aData.Length); ulong xSectorToRead = mFirstSector + xSectorNum; mFileSystem.mDevice.WriteBlock(xSectorToRead, 1, aData); }
public override DirectoryEntry GetRootDirectory() { FileSystemHelpers.Debug("FatFileSystem.GetRootDirectory", "RootCluster =" + RootCluster); var xRootEntry = new FatDirectoryEntry(this, null, mRootPath, RootCluster); return(xRootEntry); }
public FatDirectoryEntry( FatFileSystem aFileSystem, FatDirectoryEntry aParent, string aName, ulong aFirstCluster) : base(aFileSystem, aParent, aName, 0, DirectoryEntryTypeEnum.Directory) { if (aFileSystem == null) { FileSystemHelpers.Debug("FatDirectoryEntry.ctor", "aFileSystem is null."); throw new ArgumentNullException(nameof(aFileSystem)); } if (aName == null) { FileSystemHelpers.Debug("FatDirectoryEntry.ctor", "aName is null."); throw new ArgumentNullException(nameof(aName)); } if (aFirstCluster < 2) { FileSystemHelpers.Debug("FatDirectoryEntry.ctor", "aFirstCluster is out of range."); throw new ArgumentOutOfRangeException(nameof(aFirstCluster)); } FileSystemHelpers.Debug("FatDirectoryEntry.ctor", "aParent.Name =", aParent?.mName, ", aName =", aName, ", aFirstCluster =", aFirstCluster); mFileSystem = aFileSystem; mParent = aParent; mFirstClusterNum = aFirstCluster; mEntryHeaderDataOffset = 0; }
public static string GetDirectoryName(string aPath) { if (aPath != null) { CheckInvalidPathChars(aPath, false); string xPath = NormalizePath(aPath, false); int xRootLength = GetRootLength(xPath); int xNum = xPath.Length; FileSystemHelpers.Debug("GetDirectoryName of ", aPath, " xRootLength ", xRootLength, "xPathLenght " + xNum); // If lenght of aPath is the same of the lenght of the Root Path is the root Path itself! if (xNum == xRootLength) { FileSystemHelpers.Debug("Path.GetDirectoryName", "aPath =", aPath, " is the root directory"); return(null); } if (xNum > xRootLength) { while (xNum > xRootLength && xPath[--xNum] != Path.DirectorySeparatorChar && xPath[xNum] != Path.AltDirectorySeparatorChar) { } } string result = xPath.Substring(0, xNum); FileSystemHelpers.Debug("Path.GetDirectoryName", "aPath =", aPath, ", result =", result); return(result); } FileSystemHelpers.Debug("Path.GetDirectoryName", "aPath is null"); return(null); }
public override List <DirectoryEntry> GetDirectoryListing(DirectoryEntry baseDirectory) { FileSystemHelpers.Debug("FatFileSystem.GetDirectoryListing", "baseDirectory.Name =", baseDirectory?.mName); var result = new List <DirectoryEntry>(); List <FatDirectoryEntry> fatListing; if (baseDirectory == null) { // get root folder var xEntry = (FatDirectoryEntry)GetRootDirectory(); fatListing = xEntry.ReadDirectoryContents(); } else { var xEntry = (FatDirectoryEntry)baseDirectory; fatListing = xEntry.ReadDirectoryContents(); } for (int i = 0; i < fatListing.Count; i++) { result.Add(fatListing[i]); } return(result); }
public static DirectoryInfo GetParent(string aPath) { if (aPath == null) { FileSystemHelpers.Debug("Directory.GetParent", "aPath is null"); throw new ArgumentNullException("aPath"); } if (aPath.Length == 0) { FileSystemHelpers.Debug("Directory.GetParent", "aPath is empty"); throw new ArgumentException("Path must not be empty.", "aPath"); } string xFullPath = Path.GetFullPath(aPath); string xParentDirectory = Path.GetDirectoryName(xFullPath); if (xParentDirectory == null) { FileSystemHelpers.Debug("Directory.GetParent", "xParentDirectory is null"); return(null); } return(new DirectoryInfo(xParentDirectory)); }
public static string ChangeExtension(string aPath, string aExtension) { if (aPath != null) { CheckInvalidPathChars(aPath, false); string xText = aPath; int xNum = aPath.Length; while (--xNum >= 0) { char xC = aPath[xNum]; if (xC == '.') { xText = aPath.Substring(0, xNum); break; } if (xC == Path.DirectorySeparatorChar || xC == Path.AltDirectorySeparatorChar || xC == Path.VolumeSeparatorChar) { break; } } if (aExtension != null && aPath.Length != 0) { if (aExtension.Length == 0 || aExtension[0] != '.') { xText += "."; } xText += aExtension; } FileSystemHelpers.Debug("Path.ChangeExtension", "aPath =", aPath, ", aExtension =", aExtension, ", result =", xText); return(xText); } return(null); }
internal void SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata aEntryMetadata, uint aValue) { if (mParent != null) { var xData = mParent.GetDirectoryEntryData(); if (xData.Length > 0) { var xValue = new byte[aEntryMetadata.DataLength]; xValue.SetUInt32(0, aValue); uint offset = mEntryHeaderDataOffset + aEntryMetadata.DataOffset; Array.Copy(xValue, 0, xData, offset, aEntryMetadata.DataLength); FileSystemHelpers.Debug("SetDirectoryEntryMetadataValue: DataLength =", aEntryMetadata.DataLength); FileSystemHelpers.Debug("SetDirectoryEntryMetadataValue: DataOffset =", aEntryMetadata.DataOffset); FileSystemHelpers.Debug("SetDirectoryEntryMetadataValue: EntryHeaderDataOffset =", mEntryHeaderDataOffset); FileSystemHelpers.Debug("SetDirectoryEntryMetadataValue: TotalOffset =", offset); FileSystemHelpers.Debug("SetDirectoryEntryMetadataValue: aValue =", aValue); mParent.SetDirectoryEntryData(xData); } } else { throw new Exception("Root directory metadata can not be changed using the file stream."); } }
public static string CombineNoChecks(string aPath1, string aPath2) { if (aPath2.Length == 0) { FileSystemHelpers.Debug("Path.CombineNoChecks", "aPath2 has 0 length, result =", aPath1); return(aPath1); } if (aPath1.Length == 0) { FileSystemHelpers.Debug("Path.CombineNoChecks", "aPath1 has 0 length, result =", aPath2); return(aPath2); } if (IsPathRooted(aPath2)) { FileSystemHelpers.Debug("Path.CombineNoChecks", "aPath2 is root, result =", aPath2); return(aPath2); } string xResult = string.Empty; char xC = aPath1[aPath1.Length - 1]; if (xC != Path.DirectorySeparatorChar && xC != Path.AltDirectorySeparatorChar && xC != Path.VolumeSeparatorChar) { xResult = string.Concat(aPath1, "\\", aPath2); FileSystemHelpers.Debug("Path.CombineNoChecks", "aPath1 =", aPath1, ", aPath2 =", aPath2, ", result =", xResult); return(xResult); } xResult = string.Concat(aPath1, aPath2); FileSystemHelpers.Debug("Path.CombineNoChecks", "aPath1 =", aPath1, ", aPath2 =", aPath2, ", result =", xResult); return(xResult); }
private void SetDirectoryEntryData(byte[] aData) { if (aData == null) { FileSystemHelpers.Debug("FatDirectoryEntry.SetDirectoryEntryData", "aData is null."); throw new ArgumentNullException("aData"); } if (aData.Length == 0) { FileSystemHelpers.Debug("FatDirectoryEntry.SetDirectoryEntryData", "aData length is 0."); return; } FileSystemHelpers.Debug("SetDirectoryEntryData: Name =", mName); FileSystemHelpers.Debug("SetDirectoryEntryData: Size =", mSize); FileSystemHelpers.Debug("SetDirectoryEntryData: FirstClusterNum =", mFirstClusterNum); FileSystemHelpers.Debug("SetDirectoryEntryData: aData.Length =", aData.Length); if (mEntryType != DirectoryEntryTypeEnum.Unknown) { mFileSystem.Write(mFirstClusterNum, aData); } else { throw new Exception("Invalid directory entry type"); } }
public static string NormalizePath(string aPath, bool aFullCheck) { if (aPath == null) { FileSystemHelpers.Debug("Path.NormalizePath", "aPath is null"); throw new ArgumentNullException("aPath"); } string result = aPath; if (IsRelative(result)) { result = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar.ToString() + result; FileSystemHelpers.Debug("Path.NormalizePath", "aPath is relative, aPath =", aPath, ", result =", result); } if (IsDirectorySeparator(result[result.Length - 1])) { FileSystemHelpers.Debug("Found directory seprator"); result = result.Remove(result.Length - 1); } FileSystemHelpers.Debug("Path.NormalizePath", "aPath =", aPath, ", result =", result); return(result); }
public static DirectoryInfo get_Parent(DirectoryInfo aThis, [FieldAccess(Name = "$$FullPath")] ref string aFullPath) { FileSystemHelpers.Debug("DirectoryInfo.get_Parent"); var xParent = Directory.GetParent(aFullPath); return(xParent); }
public static DirectoryInfo get_Root(DirectoryInfo aThis, [FieldAccess(Name = "$$FullPath")] ref string aFullPath) { FileSystemHelpers.Debug("DirectoryInfo.get_Root"); string xRootPath = Path.GetPathRoot(aFullPath); var xRoot = new DirectoryInfo(xRootPath); return(xRoot); }
protected void Write(byte[] aBuffer, long aOffset, long aCount) { if (aCount < 0) { throw new ArgumentOutOfRangeException("aCount"); } if (aOffset < 0) { throw new ArgumentOutOfRangeException("aOffset"); } if (aBuffer == null || aBuffer.Length - aOffset < aCount) { throw new ArgumentException("Invalid offset length!"); } FileSystemHelpers.Debug("FatStream.Write", "aBuffer.Length =", aBuffer.Length, ", aOffset = ", aOffset, ", aCount = ", aCount); ulong xCount = (ulong)aCount; var xCluster = mFS.NewClusterArray(); uint xClusterSize = mFS.BytesPerCluster; long xTotalLength = (long)(mPosition + xCount); if (xTotalLength > Length) { SetLength(xTotalLength); } while (xCount > 0) { long xWriteSize; ulong xClusterIdx = mPosition / xClusterSize; ulong xPosInCluster = mPosition % xClusterSize; if (xPosInCluster + xCount > xClusterSize) { xWriteSize = (long)(xClusterSize - xPosInCluster - 1); } else { xWriteSize = (long)xCount; } mFS.Read(xClusterIdx, out xCluster); FileSystemHelpers.Debug("Writing to cluster idx", xClusterIdx); FileSystemHelpers.Debug("Writing to pos in cluster", xPosInCluster); FileSystemHelpers.Debug("Offset", aOffset); FileSystemHelpers.Debug("First byte", aBuffer[0]); Array.Copy(aBuffer, aOffset, xCluster, (long)xPosInCluster, xWriteSize); mFS.Write(mFatTable[(int)xClusterIdx], xCluster); aOffset += xWriteSize; xCount -= (ulong)xWriteSize; } mPosition += (ulong)aOffset; }
public static bool Exists(string aPath) { if (aPath == null) { return(false); } FileSystemHelpers.Debug("Directory.Exists", "aPath =", aPath); return(VFSManager.DirectoryExists(aPath)); }
public override void SetSize(long aSize) { if (mParent == null) { throw new Exception("Parent entry is null. The size cannot be set."); } FileSystemHelpers.Debug("FatDirectoryEntry.SetSize", "mName =", mName, ", mSize =", mSize, ", aSize =", aSize); SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata.Size, (uint)aSize); }
public override void SetName(string aName) { if (mParent == null) { throw new Exception("Parent entry is null. The name cannot be set."); } FileSystemHelpers.Debug("FatDirectoryEntry.SetName", "mName =", mName, ", mSize =", mSize, ", aName =", aName); SetDirectoryEntryMetadataValue(FatDirectoryEntryMetadata.ShortName, aName); }
public static List <DirectoryEntry> GetDirectoryListing(string aPath) { if (string.IsNullOrEmpty(aPath)) { throw new ArgumentNullException("aPath"); } FileSystemHelpers.Debug("VFSManager.GetDirectoryListing", "aPath = ", aPath); return(mVFS.GetDirectoryListing(aPath)); }
public static void RegisterVFS(VFSBase aVFS) { FileSystemHelpers.Debug("VFSManager.RegisterVFS"); if (mVFS != null) { throw new Exception("Virtual File System Manager already initialized!"); } aVFS.Initialize(); mVFS = aVFS; }
public static DirectoryEntry GetVolume(string aVolume) { if (string.IsNullOrEmpty(aVolume)) { throw new ArgumentNullException("aVolume"); } FileSystemHelpers.Debug("VFSManager.GetVolume", "aVolume =", aVolume); return(mVFS.GetVolume(aVolume)); }
public static DirectoryEntry CreateDirectory(string aPath) { if (string.IsNullOrEmpty(aPath)) { throw new ArgumentNullException("aPath"); } FileSystemHelpers.Debug("VFSManager.CreateDirectory", "aPath = ", aPath); return(mVFS.CreateDirectory(aPath)); }
private byte[] GetDirectoryEntryData() { FileSystemHelpers.Debug("FatDirectoryEntry.GetDirectoryEntryData"); if (mEntryType != DirectoryEntryTypeEnum.Unknown) { byte[] xData; mFileSystem.Read(mFirstClusterNum, out xData); return(xData); } throw new Exception("Invalid directory entry type"); }
public static Stream GetFileStream(string aPathname) { FileSystemHelpers.Debug("VFSManager.GetFileStream", "aPathname =", aPathname); var xFileInfo = GetFile(aPathname); if (xFileInfo == null) { throw new Exception("File not found: " + aPathname); } return(xFileInfo.GetFileStream()); }
public ulong[] GetFatChain(ulong aFirstCluster, uint aDataSize = 0) { FileSystemHelpers.Debug("Fat.GetFatChain", "aFirstCluster = ", aFirstCluster, ", aDataSize = ", aDataSize); var xReturn = new ulong[0]; ulong xCurrentCluster = aFirstCluster; ulong xValue; uint xClustersRequired = aDataSize / mFileSystem.BytesPerCluster; if (aDataSize % mFileSystem.BytesPerCluster != 0) { xClustersRequired++; } GetFatEntry(xCurrentCluster, out xValue); Array.Resize(ref xReturn, xReturn.Length + 1); xReturn[xReturn.Length - 1] = xCurrentCluster; FileSystemHelpers.Debug("Fat.GetFatChain", "xCurrentCluster =", xCurrentCluster, ", xValue =", xValue); while (!FatEntryIsEof(xValue)) { xCurrentCluster = xValue; GetFatEntry(xCurrentCluster, out xValue); Array.Resize(ref xReturn, xReturn.Length + 1); if (!FatEntryIsEof(xValue)) { xReturn[xReturn.Length - 1] = xValue; } else { xReturn[xReturn.Length - 1] = xCurrentCluster; } FileSystemHelpers.Debug("Fat.GetFatChain", "xCurrentCluster =", xCurrentCluster, ", xValue =", xValue); } if (xClustersRequired > xReturn.Length) { ulong xNewClusters = (uint)xReturn.Length - xClustersRequired; FileSystemHelpers.Debug("Fat.GetFatChain", "Allocating ", xNewClusters, " new clusters."); for (ulong i = 0; i < xNewClusters; i++) { xCurrentCluster = GetNextUnallocatedFatEntry(); ulong xLastFatEntry = xReturn[xReturn.Length - 1]; SetFatEntry(xLastFatEntry, xCurrentCluster); SetFatEntry(xCurrentCluster, FatEntryEofValue()); Array.Resize(ref xReturn, xReturn.Length + 1); xReturn[xReturn.Length - 1] = xCurrentCluster; FileSystemHelpers.Debug("Fat.GetFatChain", "xCurrentCluster =", xCurrentCluster); } } return(xReturn); }
public FatDirectoryEntry AddDirectoryEntry(string aName, DirectoryEntryTypeEnum aType) { FileSystemHelpers.Debug("FatDirectoryEntry.AddDirectoryEntry"); if ((aType == DirectoryEntryTypeEnum.Directory) || (aType == DirectoryEntryTypeEnum.File)) { uint xFirstCluster = mFileSystem.GetFat(0).GetNextUnallocatedFatEntry(); uint xEntryHeaderDataOffset = GetNextUnallocatedEntry(); var xNewEntry = new FatDirectoryEntry(mFileSystem, this, aName, 0, xFirstCluster, xEntryHeaderDataOffset, aType); xNewEntry.AllocateDirectoryEntry(); return(xNewEntry); } throw new ArgumentOutOfRangeException("aType", "Unknown directory entry type."); }
public static string GetFullPath(string aPath) { if (aPath == null) { FileSystemHelpers.Debug("Path.GetFullPath", "aPath is null"); throw new ArgumentNullException("aPath"); } string result = NormalizePath(aPath, true); FileSystemHelpers.Debug("Path.GetFullPath", "aPath =", aPath, ", result =", result); return(result); }
public static string Combine(string aPath1, string aPath2) { if (aPath1 == null || aPath2 == null) { throw new ArgumentNullException((aPath1 == null) ? "path1" : "path2"); } CheckInvalidPathChars(aPath1, false); CheckInvalidPathChars(aPath2, false); string result = CombineNoChecks(aPath1, aPath2); FileSystemHelpers.Debug("Path.Combine", "aPath1 =", aPath1, ", aPath2 =", aPath2, ", result =", result); return(result); }
private void WriteInternal(ulong aFirstCluster, byte[] aData) { if (mFatType == FatTypeEnum.Fat32) { ulong xSector = DataSector + (aFirstCluster - 2) * SectorsPerCluster; mDevice.WriteBlock(xSector, SectorsPerCluster, aData); } else { mDevice.WriteBlock(aFirstCluster, RootSectorCount, aData); } FileSystemHelpers.Debug("FatFileSystem.WriteInternal", "" + "aFirstCluster =", aFirstCluster, ", aData.Length =", aData.Length); }
//static void Init(FileStream aThis, string path, FileMode mode, FileAccess access, int rights, bool useRights, FileShare share, int bufferSize // , FileOptions options, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES secAttrs, string msgPath, bool bFromProxy) { } private static Stream InitializeStream(string aPath, FileMode aMode) { FileSystemHelpers.Debug("In FileStream.InitializeStream"); if (aPath == null) { FileSystemHelpers.Debug("In FileStream.Ctor: Path == null is true"); throw new Exception("The file path cannot be null."); } if (aPath.Length == 0) { FileSystemHelpers.Debug("In FileStream.Ctor: Path.Length == 0 is true"); throw new Exception("The file path cannot be empty."); } return(VFSManager.GetFileStream(aPath)); }