NTFileSystemAdapter: Do not handle unexpected IFileSystem exceptions

This commit is contained in:
Tal Aloni 2018-12-11 11:32:25 +02:00
parent 6cab13c1c9
commit 86afb5af33
4 changed files with 173 additions and 58 deletions

View file

@ -23,12 +23,19 @@ namespace SMBLibrary
entry = m_fileSystem.GetEntry(path);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "GetFileInformation on '{0}' failed. {1}", path, status);
result = null;
return status;
}
else
{
throw;
}
}
switch (informationClass)
{

View file

@ -6,7 +6,7 @@
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Utilities;
namespace SMBLibrary
@ -65,11 +65,18 @@ namespace SMBLibrary
entry = m_fileSystem.GetEntry(path + fileName);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "QueryDirectory: Error querying '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
entries = new List<FileSystemEntry>();
entries.Add(entry);
}

View file

@ -27,22 +27,36 @@ namespace SMBLibrary
m_fileSystem.SetAttributes(fileHandle.Path, isHidden, isReadonly, isArchived);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "SetFileInformation: Failed to set file attributes on '{0}'. {1}.", fileHandle.Path, status);
return status;
}
else
{
throw;
}
}
try
{
m_fileSystem.SetDates(fileHandle.Path, basicInformation.CreationTime, basicInformation.LastWriteTime, basicInformation.LastAccessTime);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "SetFileInformation: Failed to set file dates on '{0}'. {1}.", fileHandle.Path, status);
return status;
}
else
{
throw;
}
}
return NTStatus.STATUS_SUCCESS;
}
else if (information is FileRenameInformationType2)
@ -70,11 +84,18 @@ namespace SMBLibrary
Log(Severity.Information, "SetFileInformation: Renamed '{0}' to '{1}'", fileHandle.Path, newFileName);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "SetFileInformation: Cannot rename '{0}' to '{1}'. {2}.", fileHandle.Path, newFileName, status);
return status;
}
else
{
throw;
}
}
fileHandle.Path = newFileName;
return NTStatus.STATUS_SUCCESS;
}
@ -94,11 +115,18 @@ namespace SMBLibrary
Log(Severity.Information, "SetFileInformation: Deleted '{0}'", fileHandle.Path);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Information, "SetFileInformation: Error deleting '{0}'. {1}.", fileHandle.Path, status);
return status;
}
else
{
throw;
}
}
}
return NTStatus.STATUS_SUCCESS;
}
@ -110,11 +138,18 @@ namespace SMBLibrary
fileHandle.Stream.SetLength(allocationSize);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "SetFileInformation: Cannot set allocation for '{0}'. {1}.", fileHandle.Path, status);
return status;
}
else
{
throw;
}
}
return NTStatus.STATUS_SUCCESS;
}
else if (information is FileEndOfFileInformation)
@ -125,11 +160,18 @@ namespace SMBLibrary
fileHandle.Stream.SetLength(endOfFile);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "SetFileInformation: Cannot set end of file for '{0}'. {1}.", fileHandle.Path, status);
return status;
}
else
{
throw;
}
}
return NTStatus.STATUS_SUCCESS;
}
else

View file

@ -62,11 +62,18 @@ namespace SMBLibrary
{
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "CreateFile: Error retrieving '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
if (createDisposition == CreateDisposition.FILE_OPEN)
{
@ -115,11 +122,18 @@ namespace SMBLibrary
}
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
fileStatus = FileStatus.FILE_CREATED;
}
else if (createDisposition == CreateDisposition.FILE_OPEN_IF ||
@ -153,11 +167,18 @@ namespace SMBLibrary
}
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
fileStatus = FileStatus.FILE_CREATED;
}
else
@ -192,11 +213,18 @@ namespace SMBLibrary
temp.Close();
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "CreateFile: Error truncating '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
fileStatus = FileStatus.FILE_OVERWRITTEN;
}
else if (createDisposition == CreateDisposition.FILE_SUPERSEDE)
@ -207,11 +235,18 @@ namespace SMBLibrary
m_fileSystem.Delete(path);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "CreateFile: Error deleting '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
try
{
@ -227,11 +262,18 @@ namespace SMBLibrary
}
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "CreateFile: Error creating '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
fileStatus = FileStatus.FILE_SUPERSEDED;
}
}
@ -281,11 +323,18 @@ namespace SMBLibrary
stream = m_fileSystem.OpenFile(path, FileMode.Open, fileAccess, fileShare, fileOptions);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "OpenFile: Cannot open '{0}', Access={1}, Share={2}. NTStatus: {3}.", path, fileAccess, fileShareString, status);
return status;
}
else
{
throw;
}
}
Log(Severity.Information, "OpenFileStream: Opened '{0}', Access={1}, Share={2}, FileOptions={3}", path, fileAccess, fileShareString, fileOptionsString);
return NTStatus.STATUS_SUCCESS;
@ -337,11 +386,18 @@ namespace SMBLibrary
bytesRead = stream.Read(data, 0, maxCount);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "ReadFile: Cannot read '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
if (bytesRead < maxCount)
{
@ -369,11 +425,18 @@ namespace SMBLibrary
stream.Write(data, 0, data.Length);
}
catch (Exception ex)
{
if (ex is IOException || ex is UnauthorizedAccessException)
{
NTStatus status = ToNTStatus(ex);
Log(Severity.Verbose, "WriteFile: Cannot write '{0}'. {1}.", path, status);
return status;
}
else
{
throw;
}
}
numberOfBytesWritten = data.Length;
return NTStatus.STATUS_SUCCESS;
}
@ -444,11 +507,7 @@ namespace SMBLibrary
/// <param name="exception">IFileSystem exception</param>
private static NTStatus ToNTStatus(Exception exception)
{
if (exception is ArgumentException)
{
return NTStatus.STATUS_OBJECT_PATH_SYNTAX_BAD;
}
else if (exception is DirectoryNotFoundException)
if (exception is DirectoryNotFoundException)
{
return NTStatus.STATUS_OBJECT_PATH_NOT_FOUND;
}