singrdk/base/Services/RamDisk/Control/CreateCommand.sg

95 lines
2.9 KiB
Plaintext

///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// File: CreateCommand.sg
//
// Note: Command to create a new RAM disk of a given size.
//
using System;
using Microsoft.SingSharp;
using Microsoft.SingSharp.Reflection;
using Microsoft.Singularity.Applications;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.Configuration;
using Microsoft.Singularity.Directory;
using Microsoft.Singularity.Io;
using Microsoft.Singularity.Services.RamDisk.Contracts;
using FileSystem.Utils;
namespace Microsoft.Singularity.Services.RamDisk.RamDiskControl
{
[ConsoleCategory(Action = "create",
DefaultAction = false,
HelpMessage = "Create and register a new RAM disk device.")]
internal class CreateCommand
{
[InputEndpoint("data")]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdin;
[OutputEndpoint("data")]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
[StringParameter("s", Default = "Automatic", Mandatory = true,
HelpMessage = "Disk size")]
string DiskSizeStr;
reflective internal CreateCommand();
internal int AppMain()
{
ulong diskSizeBytes;
try {
diskSizeBytes = DiskSizeUtils.ParsePrettySizeString(DiskSizeStr);
}
catch (FormatException) {
return -1;
}
RamDiskControlContract.Imp imp = Utilities.ConnectToManager();
if (imp != null) {
return DoCreate(imp, diskSizeBytes);
}
return -1;
}
private static
int DoCreate(
[Claims] RamDiskControlContract.Imp! controller,
ulong diskSizeBytes
)
{
try {
controller.SendCreate(diskSizeBytes);
switch receive {
case controller.CreateSuccess(diskPath):
Console.WriteLine(
"Created disk {0}\n" +
"Capacity: {1}",
Bitter.ToString2(diskPath),
DiskSizeUtils.GetPrettySizeString(diskSizeBytes));
delete diskPath;
return 0;
case controller.Fail(error):
Utilities.DisplayError(error);
return -1;
case controller.ChannelClosed():
Utilities.DisplayChannelClosedError();
return -1;
}
}
finally {
delete controller;
}
}
}
}