singrdk/base/Applications/utilities/delete/delete.sg

140 lines
4.7 KiB
Plaintext
Raw Normal View History

2008-03-05 09:52:00 -05:00
///////////////////////////////////////////////////////////////////////////////
//
// Microsoft Research Singularity
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Note:
//
using FileSystem.Utils;
using System;
using System.Text;
using System.Threading;
using Microsoft.Singularity;
using Microsoft.Singularity.Directory;
using Microsoft.Singularity.Channels;
using Microsoft.Singularity.FileSystem;
using Microsoft.Singularity.V1.Services;
using Microsoft.Contracts;
using Microsoft.SingSharp.Reflection;
using Microsoft.Singularity.Applications;
using Microsoft.Singularity.Io;
using Microsoft.Singularity.Configuration;
[assembly: Transform(typeof(ApplicationResourceTransform))]
namespace Microsoft.Singularity.Applications
{
[ConsoleCategory(HelpMessage="delete named file", DefaultAction=true)]
internal class Parameters
{
[InputEndpoint("data")]
public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
[OutputEndpoint("data")]
public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
[Endpoint]
public readonly TRef<DirectoryServiceContract.Imp:Start> nsRef;
[StringParameter( "filename", Mandatory=true, Position=0, HelpMessage="Name of file.")]
internal string fileName;
reflective internal Parameters();
internal int AppMain() {
return FsDelete.AppMain(this);
}
}
public class FsDelete
{
internal static bool DeleteFile(string! fileName, DirectoryServiceContract.Imp! ds)
{
ErrorCode error;
FileUtils.DeleteFile(fileName, ds, out error);
bool ok = (error == ErrorCode.NoError);
if (!ok) {
Console.WriteLine(" File ({0}) delete failed. reason:{1}",
fileName, SdsUtils.ErrorCodeToString(error) );
}
return ok;
}
internal static bool DeleteLink(string! fileName, DirectoryServiceContract.Imp! ds)
{
ErrorCode error;
bool ok = SdsUtils.DeleteLink(fileName, ds, out error);
if (!ok) {
Console.WriteLine(" Symbolic Link ({0}) delete failed. reason:{1}",
fileName, SdsUtils.ErrorCodeToString(error) );
}
#if DEBUG
else {
DebugStub.Break();
}
#endif
return ok;
}
internal static bool DeleteDirectory(string! fileName, DirectoryServiceContract.Imp! ds)
{
ErrorCode error;
bool ok = SdsUtils.DeleteDirectory(fileName, ds, out error);
if (!ok) {
Console.WriteLine("Delete of directory ({0}) failed. reason:{1}",
fileName, SdsUtils.ErrorCodeToString(error) );
}
return ok;
}
internal static int AppMain(Parameters! config)
{
2008-11-17 18:29:00 -05:00
DirectoryServiceContract.Imp ds = ((!)config.nsRef).Acquire();
2008-03-05 09:52:00 -05:00
if (ds == null) {
throw new Exception("Unable to acquire handle to the Directory Service root");
}
ds.RecvSuccess();
// see what kind of entity the path points to.
ErrorCode error;
2008-11-17 18:29:00 -05:00
FileAttributesRecord fileAttributes;
bool ok = SdsUtils.GetAttributes((!)config.fileName, ds, out fileAttributes, out error);
2008-03-05 09:52:00 -05:00
if (!ok) {
Console.WriteLine(" File ({0}) not found. reason:{1}",
config.fileName, SdsUtils.ErrorCodeToString(error) );
#if DEBUG
DebugStub.Break();
#endif
delete ds;
return -1;
}
bool success = false;
2008-11-17 18:29:00 -05:00
switch (fileAttributes.Type) {
2008-03-05 09:52:00 -05:00
case NodeType.File:
success = DeleteFile(config.fileName, ds);
break;
case NodeType.Directory:
success = DeleteDirectory(config.fileName, ds);
break;
case NodeType.SymLink:
Console.WriteLine("Warning delete link in /fs will hand system");
success = DeleteLink(config.fileName, ds);
break;
default:
Console.WriteLine(" File ({0}) is of type '({1}). Unable to delete",
2008-11-17 18:29:00 -05:00
config.fileName, SdsUtils.NodeTypeToString(fileAttributes.Type) );
2008-03-05 09:52:00 -05:00
success = false;
break;
}
delete ds;
return success ? 0 : 1;
}
} // class Test
}