143 lines
4.7 KiB
Plaintext
143 lines
4.7 KiB
Plaintext
|
///////////////////////////////////////////////////////////////////////////////
|
||
|
//
|
||
|
// Microsoft Research Singularity
|
||
|
//
|
||
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||
|
//
|
||
|
// File: Delete.sg
|
||
|
//
|
||
|
// 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)
|
||
|
{
|
||
|
DirectoryServiceContract.Imp ds = (config.nsRef).Acquire();
|
||
|
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;
|
||
|
NodeType nodeType;
|
||
|
long size;
|
||
|
bool ok = SdsUtils.GetAttributes(config.fileName, ds, out size, out nodeType, out error);
|
||
|
|
||
|
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;
|
||
|
switch (nodeType) {
|
||
|
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",
|
||
|
config.fileName, SdsUtils.NodeTypeToString(nodeType) );
|
||
|
success = false;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
delete ds;
|
||
|
return success ? 0 : 1;
|
||
|
}
|
||
|
} // class Test
|
||
|
}
|