Skip to content

Commit

Permalink
Refactors and fixes from sonarcloud analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
josesimoes committed Jun 7, 2024
1 parent ff143af commit f3635e0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
8 changes: 8 additions & 0 deletions System.IO.FileSystem/DirectoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ public void Delete(bool recursive)
recursive);
}

/// <inheritdoc/>
protected override void HandleRefreshError()
{
throw new IOException(
string.Empty,
(int)IOException.IOExceptionErrorCode.DirectoryNotFound);
}

/// <summary>
/// Returns the original path that was passed to the <see cref="DirectoryInfo"/> constructor. Use the <see cref="FileSystemInfo.FullName"/> or <see cref="Name"/> properties for the full path or file/directory name instead of this method.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion System.IO.FileSystem/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ internal static void Copy(

byte[] buffer = new byte[ChunkSize];

for (; ; )
while (true)
{
int readSize = reader.Read(buffer, 0, ChunkSize);

Expand Down
10 changes: 9 additions & 1 deletion System.IO.FileSystem/FileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public long Length
{
RefreshIfNull();

return (long)_nativeFileInfo.Size;
return _nativeFileInfo.Size;
}
}

Expand Down Expand Up @@ -91,6 +91,14 @@ public override bool Exists
}
}

/// <inheritdoc/>
protected override void HandleRefreshError()
{
throw new IOException(
string.Empty,
(int)IOException.IOExceptionErrorCode.FileNotFound);
}

/// <summary>
/// Returns the original path that was passed to the FileInfo constructor. Use the <see cref="FileSystemInfo.FullName"/> or <see cref="Name"/> property for the full path or file name.
/// </summary>
Expand Down
13 changes: 7 additions & 6 deletions System.IO.FileSystem/FileSystemInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public abstract string Name
/// Refreshes the state of the object.
/// </summary>
/// <exception cref="IOException">A device such as a disk drive is not ready.</exception>
public void Refresh()
public virtual void Refresh()
{
object record = FileSystemManager.AddToOpenListForRead(_fullPath);

Expand All @@ -90,11 +90,7 @@ public void Refresh()

if (_nativeFileInfo == null)
{
IOException.IOExceptionErrorCode errorCode = (this is FileInfo) ? IOException.IOExceptionErrorCode.FileNotFound : IOException.IOExceptionErrorCode.DirectoryNotFound;

throw new IOException(
string.Empty,
(int)errorCode);
HandleRefreshError();
}
}
finally
Expand All @@ -103,6 +99,11 @@ public void Refresh()
}
}

/// <summary>
/// Handler for the case when the file or directory does not exist.
/// </summary>
protected abstract void HandleRefreshError();

internal void RefreshIfNull()
{
if (_nativeFileInfo == null)
Expand Down
12 changes: 6 additions & 6 deletions System.IO.FileSystem/FileSystemManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ public FileRecord(
}
}

public static object AddToOpenList(string fullName)
public static object AddToOpenListForRead(string fullName)
{
return AddToOpenList(
fullName,
FileAccess.ReadWrite,
FileShare.None);
FileAccess.Read,
FileShare.ReadWrite);
}

public static object AddToOpenListForRead(string fullName)
public static object AddToOpenList(string fullName)
{
return AddToOpenList(
fullName,
FileAccess.Read,
FileShare.ReadWrite);
FileAccess.ReadWrite,
FileShare.None);
}

public static FileRecord AddToOpenList(
Expand Down
2 changes: 2 additions & 0 deletions System.IO.FileSystem/NativeFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ internal class NativeFileStream
// field is required for native interop
#pragma warning disable IDE0051
#pragma warning disable CS0169
#pragma warning disable S1144
object _fs;
#pragma warning restore S1144
#pragma warning restore CS0169
#pragma warning restore IDE0051

Expand Down

0 comments on commit f3635e0

Please sign in to comment.