Skip to content

Commit

Permalink
Merge pull request CosmosOS#955 from fanoI/driveinfo_work
Browse files Browse the repository at this point in the history
Driveinfo work
  • Loading branch information
fanoI authored Apr 22, 2018
2 parents 611ca9d + d97d3b6 commit 57dc781
Show file tree
Hide file tree
Showing 14 changed files with 769 additions and 90 deletions.
136 changes: 136 additions & 0 deletions Tests/Cosmos.Kernel.Tests.Fat/System.IO/DriveInfoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System.IO;
using Cosmos.TestRunner;
using Cosmos.Debug.Kernel;
using System;

namespace Cosmos.Kernel.Tests.Fat.System.IO
{
public class DriveInfoTest
{
/// <summary>
/// Tests System.IO.DriveInfo plugs.
/// </summary>
public static void Execute(Debugger mDebugger)
{
string driveName = @"0:\";
var MyDrive = new DriveInfo(driveName);

mDebugger.Send("START TEST: Get Name");

Assert.IsTrue(MyDrive.Name == driveName, "DriveInfo.Name failed drive has wrong name");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Get TotalSize");
long MyDriveSize = MyDrive.TotalSize;

mDebugger.Send($"Size is {MyDriveSize}");

Assert.IsTrue(MyDriveSize > 0, "DriveInfo.TotalSize failed: invalid size");
mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Get AvailableFreeSpace");
long MyDriveAvailableFreeSpace = MyDrive.AvailableFreeSpace;

mDebugger.Send($"AvailableFreeSpace {MyDriveAvailableFreeSpace}");

Assert.IsTrue(MyDriveAvailableFreeSpace >= 0, "DriveInfo.AvailableFreeSpace failed: invalid size");

Assert.IsFalse(MyDriveAvailableFreeSpace > MyDrive.TotalSize, "DriveInfo.AvailableFreeSpace failed: more than TotalSize");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Get TotalFreeSpace");
long MyDriveTotalFreeSpace = MyDrive.TotalFreeSpace;

mDebugger.Send($"TotalFreeSpace {MyDriveTotalFreeSpace}");

Assert.IsTrue(MyDriveTotalFreeSpace >= 0, "DriveInfo.TotalFreeSpace failed: invalid size");

Assert.IsFalse(MyDriveTotalFreeSpace > MyDrive.TotalSize, "DriveInfo.TotalFreeSpace failed: more than TotalSize");

/*
* If disk quotas are enabled AvailableFreeSpace and TotalFreeSpace could be different numbers but TotalFreeSpace
* should be always >= of AvailableFreeSpace
*/
Assert.IsTrue(MyDriveTotalFreeSpace >= MyDriveAvailableFreeSpace, "DriveInfo.MyDriveTotalFreeSpace failed: less than AvailableFreeSpace");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Get RootDirectory");

var xDi = MyDrive.RootDirectory;

Assert.IsTrue(xDi.Name == MyDrive.Name, "RootDirectory failed");
mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Get DriveFormat");

Assert.IsTrue(MyDrive.DriveFormat == "FAT32", "DriveFormat failed");
mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Get VolumeLabel");

string OriginalVolumeLabel = MyDrive.VolumeLabel;

mDebugger.Send($"Volume Label of {MyDrive.Name} is {MyDrive.VolumeLabel}");

Assert.IsTrue(OriginalVolumeLabel == "COSMOS", "VolumeLabel Get failed not the expected value");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Set VolumeLabel to <TEST>");
// Now try to change it...
String NewVolumeLabel = "TEST";
mDebugger.Send($"Changing Volume Label to {NewVolumeLabel}...");
MyDrive.VolumeLabel = NewVolumeLabel;

string SetVolumeLabel = MyDrive.VolumeLabel;

mDebugger.Send($"Volume Label of {MyDrive.Name} is {SetVolumeLabel}");

Assert.IsTrue(SetVolumeLabel == NewVolumeLabel, "VolumeLabel Set failed: not the expected value");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Set VolumeLabel (restoring original label)");
// Now try to change it...
mDebugger.Send($"Changing Volume Label to {OriginalVolumeLabel}...");
MyDrive.VolumeLabel = OriginalVolumeLabel;

SetVolumeLabel = MyDrive.VolumeLabel;

mDebugger.Send($"Volume Label of {MyDrive.Name} is {SetVolumeLabel}");

Assert.IsTrue(SetVolumeLabel == OriginalVolumeLabel, "VolumeLabel Set failed: not the expected value");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Testing isReady status of the Drive");

Assert.IsTrue(MyDrive.IsReady, "IsReady failed drive not ready");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Testing DriveType");

Assert.IsTrue(MyDrive.DriveType == DriveType.Fixed, "DriveType failed drive not of Fixed type");

mDebugger.Send("END TEST");

mDebugger.Send("START TEST: Getting all drives info");

DriveInfo[] xDrives = DriveInfo.GetDrives();

Assert.IsFalse(xDrives == null, "GetDrives failed: null array returned");

// I suppose that at least a drive is recognized by Cosmos
Assert.IsTrue(xDrives.Length > 0, "GetDrives failed: no drive recognized");

/* Check that only the Name property of the first one is the same of driveName */
Assert.IsTrue(xDrives[0].Name == driveName, "GetDrives failed: first drive is not the same of MyDrive (0:)");

mDebugger.Send("END TEST");
}
}
}
1 change: 1 addition & 0 deletions Tests/Cosmos.Kernel.Tests.Fat2/Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected override void Run()
mDebugger.Send("Run");

FileTest.Execute(mDebugger);
DriveInfoTest.Execute(mDebugger);

TestController.Completed();
}
Expand Down
99 changes: 86 additions & 13 deletions source/Cosmos.System2/FileSystem/CosmosVFS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ namespace Cosmos.System.FileSystem
public class CosmosVFS : VFSBase
{
private List<Partition> mPartitions;


private List<FileSystem> mFileSystems;


private FileSystem mCurrentFileSystem;
private List<FileSystemFactory> mRegisteredFileSystems;

/// <summary>
/// Initializes the virtual file system.
Expand All @@ -33,6 +30,9 @@ public override void Initialize()
{
mPartitions = new List<Partition>();
mFileSystems = new List<FileSystem>();
mRegisteredFileSystems = new List<FileSystemFactory>();

RegisterFileSystem(new FatFileSystemFactory());

InitializePartitions();
if (mPartitions.Count > 0)
Expand All @@ -41,6 +41,12 @@ public override void Initialize()
}
}

public override void RegisterFileSystem(FileSystemFactory aFileSystemFactory)
{
Global.mFileSystemDebugger.SendInternal($"Registering filesystem {aFileSystemFactory.Name}");
mRegisteredFileSystems.Add(aFileSystemFactory);
}

/// <summary>
/// Creates a new file.
/// </summary>
Expand Down Expand Up @@ -400,17 +406,18 @@ protected virtual void InitializeFileSystems()
{
string xRootPath = string.Concat(i, VolumeSeparatorChar, DirectorySeparatorChar);
var xSize = (long)(mPartitions[i].BlockCount * mPartitions[i].BlockSize / 1024 / 1024);
switch (FileSystem.GetFileSystemType(mPartitions[i]))

// We 'probe' the partition <i> with all the FileSystem registered until we find a Filesystem that can read / write to it
foreach (var fs in mRegisteredFileSystems)
{
case FileSystemType.FAT:
if (fs.IsType(mPartitions[i]))
{
Global.mFileSystemDebugger.SendInternal($"Partion {i} has a {fs.Name} filesystem");
mFileSystems.Add(new FatFileSystem(mPartitions[i], xRootPath, xSize));
break;
default:
global::System.Console.WriteLine("Unknown filesystem type!");
return;
}
}

if ((mFileSystems.Count > 0) && (mFileSystems[mFileSystems.Count - 1].mRootPath == xRootPath))
if ((mFileSystems.Count > 0) && (mFileSystems[mFileSystems.Count - 1].RootPath == xRootPath))
{
string xMessage = string.Concat("Initialized ", mFileSystems.Count, " filesystem(s)...");
global::System.Console.WriteLine(xMessage);
Expand Down Expand Up @@ -447,15 +454,15 @@ private FileSystem GetFileSystemFromPath(string aPath)
Global.mFileSystemDebugger.SendInternal("xPath after GetPathRoot =");
Global.mFileSystemDebugger.SendInternal(xPath);

if ((mCurrentFileSystem != null) && (xPath == mCurrentFileSystem.mRootPath))
if ((mCurrentFileSystem != null) && (xPath == mCurrentFileSystem.RootPath))
{
Global.mFileSystemDebugger.SendInternal("Returning current file system.");
return mCurrentFileSystem;
}

for (int i = 0; i < mFileSystems.Count; i++)
{
if (mFileSystems[i].mRootPath == xPath)
if (mFileSystems[i].RootPath == xPath)
{
Global.mFileSystemDebugger.SendInternal("Found filesystem.");
mCurrentFileSystem = mFileSystems[i];
Expand Down Expand Up @@ -547,5 +554,71 @@ private DirectoryEntry GetVolume(FileSystem aFS)

return aFS.GetRootDirectory();
}

/// <summary>
/// Verifies if driveId is a valid id for a drive.
/// </summary>
/// <param name="driveId">The id of the drive.</param>
/// <returns>true if the drive id is valid, false otherwise.</returns>
public override bool IsValidDriveId(string driveId)
{
Global.mFileSystemDebugger.SendInternal($"driveId is {driveId} after normalization");

/* We need to remove ':\' to get only the numeric value */
driveId = driveId.Remove(driveId.Length - 2);
Global.mFileSystemDebugger.SendInternal($"driveId is now {driveId}");

/*
* Cosmos Drive name is really similar to DOS / Windows but a number instead of a letter is used, it is not limited
* to 1 character but any number is valid
*/

bool isOK = Int32.TryParse(driveId, out int val);
Global.mFileSystemDebugger.SendInternal($"isOK is {isOK}");

return isOK;
}

public override long GetTotalSize(string aDriveId)
{
var xFs = GetFileSystemFromPath(aDriveId);

/* We have to return it in bytes */
return xFs.Size * 1024 * 1024;
}

public override long GetAvailableFreeSpace(string aDriveId)
{
var xFs = GetFileSystemFromPath(aDriveId);

return xFs.AvailableFreeSpace;
}

public override long GetTotalFreeSpace(string aDriveId)
{
var xFs = GetFileSystemFromPath(aDriveId);

return xFs.TotalFreeSpace;
}

public override string GetFileSystemType(string aDriveId)
{
var xFs = GetFileSystemFromPath(aDriveId);

return xFs.Type;
}

public override string GetFileSystemLabel(string aDriveId)
{
var xFs = GetFileSystemFromPath(aDriveId);

return xFs.Label;
}

public override void SetFileSystemLabel(string aDriveId, string aLabel)
{
var xFs = GetFileSystemFromPath(aDriveId);
xFs.Label = aLabel;
}
}
}
Loading

0 comments on commit 57dc781

Please sign in to comment.