public static FileInformation ToFileInformation(SetInformation information) { switch (information) { case SetFileBasicInfo info: { SetFileBasicInfo basicInfo = info; FileBasicInformation fileBasicInfo = new FileBasicInformation { CreationTime = basicInfo.CreationTime, LastAccessTime = basicInfo.LastAccessTime, LastWriteTime = basicInfo.LastWriteTime, ChangeTime = basicInfo.LastChangeTime, FileAttributes = (FileAttributes)basicInfo.ExtFileAttributes, Reserved = basicInfo.Reserved }; return(fileBasicInfo); } case SetFileDispositionInfo info: { FileDispositionInformation fileDispositionInfo = new FileDispositionInformation { DeletePending = info.DeletePending }; return(fileDispositionInfo); } case SetFileAllocationInfo info: { // This information level is used to set the file length in bytes. // Note: the input will NOT be a multiple of the cluster size / bytes per sector. FileAllocationInformation fileAllocationInfo = new FileAllocationInformation { AllocationSize = info.AllocationSize }; return(fileAllocationInfo); } case SetFileEndOfFileInfo info: { FileEndOfFileInformation fileEndOfFileInfo = new FileEndOfFileInformation { EndOfFile = info.EndOfFile }; return(fileEndOfFileInfo); } default: throw new NotImplementedException(); } }
/// <summary> /// Delete Folder / File. /// </summary> /// <param name="node"></param> /// <returns></returns> public bool Delete(Node node) { if (!this.ValidateNode(node)) { return(false); } if (node.Type == NodeType.Server) { this.AddError("Delete", $"Invalid Operation: NodeType.{node.Type}"); return(false); } if (node.PathSet.Elements.Length <= 0) { this.AddError("Delete", $"Shared Folder Cannot Delete: {node.FullPath}"); return(false); } var path = this.FormatPath(node.PathSet.ElementsPath); using (var hdr = this.GetHandler(path, HandleType.Delete, node.Type)) { if (!hdr.Succeeded) { this.AddError("Delete", $"Create Handle Failed: {node.FullPath}"); return(false); } var info = new FileDispositionInformation { DeletePending = true }; var status = this.Store.SetFileInformation(hdr.Handle, info); if (status != NTStatus.STATUS_SUCCESS) { var message = (status == NTStatus.STATUS_DIRECTORY_NOT_EMPTY) ? $"Directory is Not Empty: {node.FullPath}" : $"Delete Failed: {node.FullPath}"; this.AddError("Delete", message); return(false); } return(true); } }
public void DeleteFile(string fileName, bool isDirectory, bool recursive) { if (recursive && isDirectory) { foreach (var item in EnumerateFileEntries(fileName, "*", SearchOption.TopDirectoryOnly)) { DeleteFile(item.FileName, item.FileAttributes.HasFlag(SMBLibrary.FileAttributes.Directory), true); } } var handle = OpenFile(fileName, isDirectory, FileMode.Open, FileAccess.ReadWrite, FileShare.None, true); FileDispositionInformation fileDispositionInformation = new FileDispositionInformation(); fileDispositionInformation.DeletePending = true; ThrowOnError(FileStore.SetFileInformation(handle, fileDispositionInformation)); ThrowOnError(FileStore.CloseFile(handle)); }
public static NTStatus Delete(INTFileStore fileStore, string path, CreateOptions createOptions, SecurityContext securityContext) { object handle; FileStatus fileStatus; ShareAccess shareAccess = ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete; NTStatus status = fileStore.CreateFile(out handle, out fileStatus, path, AccessMask.DELETE, 0, shareAccess, CreateDisposition.FILE_OPEN, createOptions, securityContext); if (status != NTStatus.STATUS_SUCCESS) { return(status); } FileDispositionInformation fileDispositionInfo = new FileDispositionInformation(); fileDispositionInfo.DeletePending = true; status = fileStore.SetFileInformation(handle, fileDispositionInfo); fileStore.CloseFile(handle); return(status); }
private void AlternateDataStream_DeleteStream(FileType fileType) { //Prerequisites: Create streams on a newly created file // Step 1: Delete the Alternate Data Stream <Stream2> BaseTestSite.Log.Add(LogEntryKind.TestStep, "{0}. Delete the Alternate Data Stream with name: " + dataStreamName2, ++testStep); dataStreamList.Remove(":" + dataStreamName2 + ":$DATA"); BaseTestSite.Log.Add(LogEntryKind.Debug, "Set FileDispositionInformation.DeletePending to 1."); FileDispositionInformation fileDispositionInfo = new FileDispositionInformation(); fileDispositionInfo.DeletePending = 1; List <byte> byteList = new List <byte>(); byteList.AddRange(BitConverter.GetBytes(fileDispositionInfo.DeletePending)); status = this.fsaAdapter.SetFileInformation( FileInfoClass.FILE_DISPOSITION_INFORMATION, byteList.ToArray()); this.fsaAdapter.AssertIfNotSuccess(status, "Set FileDispositionInformation.DeletePending operation failed"); BaseTestSite.Log.Add(LogEntryKind.Debug, "Close the open to delete the stream."); status = this.fsaAdapter.CloseOpen(); this.fsaAdapter.AssertIfNotSuccess(status, "Close open operation failed"); this.fsaAdapter.AssertIfNotSuccess(status, "Delete the Alternate Data Stream operation failed"); //Step 2: Create a new open for the File, it could be a DataFile or a DirectoryFile BaseTestSite.Log.Add(LogEntryKind.TestStep, "{0}. Create a new open for the file with type: " + fileType.ToString() + " and name: " + fileName, ++testStep); createFileType = (fileType == FileType.DataFile ? CreateOptions.NON_DIRECTORY_FILE : CreateOptions.DIRECTORY_FILE); status = this.fsaAdapter.CreateFile( fileName, FileAttribute.NORMAL, createFileType, FileAccess.GENERIC_ALL, ShareAccess.FILE_SHARE_READ | ShareAccess.FILE_SHARE_WRITE | ShareAccess.FILE_SHARE_DELETE, CreateDisposition.OPEN_IF); this.fsaAdapter.AssertIfNotSuccess(status, "Create file operation failed"); }
public static FileInformation ToFileInformation(SetInformation information) { if (information is SetFileBasicInfo) { SetFileBasicInfo basicInfo = (SetFileBasicInfo)information; FileBasicInformation fileBasicInfo = new FileBasicInformation(); fileBasicInfo.CreationTime = basicInfo.CreationTime; fileBasicInfo.LastAccessTime = basicInfo.LastAccessTime; fileBasicInfo.LastWriteTime = basicInfo.LastWriteTime; fileBasicInfo.ChangeTime = basicInfo.LastChangeTime; fileBasicInfo.FileAttributes = (FileAttributes)basicInfo.ExtFileAttributes; fileBasicInfo.Reserved = basicInfo.Reserved; return(fileBasicInfo); } else if (information is SetFileDispositionInfo) { FileDispositionInformation fileDispositionInfo = new FileDispositionInformation(); fileDispositionInfo.DeletePending = ((SetFileDispositionInfo)information).DeletePending; return(fileDispositionInfo); } else if (information is SetFileAllocationInfo) { // This information level is used to set the file length in bytes. // Note: the input will NOT be a multiple of the cluster size / bytes per sector. FileAllocationInformation fileAllocationInfo = new FileAllocationInformation(); fileAllocationInfo.AllocationSize = ((SetFileAllocationInfo)information).AllocationSize; return(fileAllocationInfo); } else if (information is SetFileEndOfFileInfo) { FileEndOfFileInformation fileEndOfFileInfo = new FileEndOfFileInformation(); fileEndOfFileInfo.EndOfFile = ((SetFileEndOfFileInfo)information).EndOfFile; return(fileEndOfFileInfo); } else { throw new NotImplementedException(); } }
public static NTStatus SetFileInformation(INTFileStore fileStore, object handle, SetInformation information) { if (information is SetFileBasicInfo) { SetFileBasicInfo basicInfo = (SetFileBasicInfo)information; FileBasicInformation fileBasicInfo = new FileBasicInformation(); fileBasicInfo.CreationTime = basicInfo.CreationTime; fileBasicInfo.LastAccessTime = basicInfo.LastAccessTime; fileBasicInfo.LastWriteTime = basicInfo.LastWriteTime; fileBasicInfo.ChangeTime = basicInfo.LastChangeTime; fileBasicInfo.FileAttributes = (FileAttributes)basicInfo.ExtFileAttributes; fileBasicInfo.Reserved = basicInfo.Reserved; return(fileStore.SetFileInformation(handle, fileBasicInfo)); } else if (information is SetFileDispositionInfo) { FileDispositionInformation fileDispositionInfo = new FileDispositionInformation(); fileDispositionInfo.DeletePending = ((SetFileDispositionInfo)information).DeletePending; return(fileStore.SetFileInformation(handle, fileDispositionInfo)); } else if (information is SetFileAllocationInfo) { // This information level is used to set the file length in bytes. // Note: the input will NOT be a multiple of the cluster size / bytes per sector. FileAllocationInformation fileAllocationInfo = new FileAllocationInformation(); fileAllocationInfo.AllocationSize = ((SetFileAllocationInfo)information).AllocationSize; return(fileStore.SetFileInformation(handle, fileAllocationInfo)); } else if (information is SetFileEndOfFileInfo) { FileEndOfFileInformation fileEndOfFileInfo = new FileEndOfFileInformation(); fileEndOfFileInfo.EndOfFile = ((SetFileEndOfFileInfo)information).EndOfFile; return(fileStore.SetFileInformation(handle, fileEndOfFileInfo)); } else { return(NTStatus.STATUS_NOT_IMPLEMENTED); } }
public static NTStatus Delete(INTFileStore fileStore, string path, CreateOptions createOptions, SecurityContext securityContext) { object handle; FileStatus fileStatus; NTStatus openStatus = fileStore.CreateFile(out handle, out fileStatus, path, DirectoryAccessMask.DELETE, 0, CreateDisposition.FILE_OPEN, createOptions, securityContext); if (openStatus != NTStatus.STATUS_SUCCESS) { return(openStatus); } FileDispositionInformation fileDispositionInfo = new FileDispositionInformation(); fileDispositionInfo.DeletePending = true; NTStatus setStatus = fileStore.SetFileInformation(handle, fileDispositionInfo); if (setStatus != NTStatus.STATUS_SUCCESS) { return(setStatus); } NTStatus closeStatus = fileStore.CloseFile(handle); return(closeStatus); }
public void DeleteFile(string path) { Debug.WriteLine("Delete " + path); //if (!path.StartsWith("sebastian")) throw new Exception(); NTStatus status = fileStore.CreateFile(out object fileHandle, out FileStatus fileStatus, path, AccessMask.GENERIC_WRITE | AccessMask.DELETE | AccessMask.SYNCHRONIZE, FileAttributes.Normal, ShareAccess.None, CreateDisposition.FILE_OPEN, 0, null); if (status != NTStatus.STATUS_SUCCESS) { throw new IOException($"Failed to delete file: {status}"); } FileDispositionInformation fileDispositionInformation = new FileDispositionInformation(); fileDispositionInformation.DeletePending = true; status = fileStore.SetFileInformation(fileHandle, fileDispositionInformation); fileStore.CloseFile(fileHandle); if (status != NTStatus.STATUS_SUCCESS) { throw new IOException($"Failed to delete file: {status}"); } }