Skip to content

Commit

Permalink
yar
Browse files Browse the repository at this point in the history
  • Loading branch information
scottoneil-ms committed Feb 27, 2024
1 parent 9febeee commit a481c81
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 54 deletions.
20 changes: 4 additions & 16 deletions src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -666,30 +666,18 @@ private async Task<bool> EnumerateFilesFromArtifactsProvider(TContext globalCont
continue;
}

long artifactSize = 0;
try
{
artifactSize = artifact.SizeInBytes.Value;
}
catch (Exception e) when (e is IOException || e is ArgumentException)
{
DriverEventSource.Log.ArtifactNotScanned(filePath, DriverEventNames.FilePathNotAllowed, 00, data2: null);
Notes.LogFileSkipped(globalContext, filePath, e.Message);
continue;
}

if (artifactSize == 0)
if (artifact.SizeInBytes == 0)
{
DriverEventSource.Log.ArtifactNotScanned(filePath, DriverEventNames.EmptyFile, 00, data2: null);
Notes.LogEmptyFileSkipped(globalContext, filePath);
continue;
}

if (!IsTargetWithinFileSizeLimit(artifactSize, globalContext.MaxFileSizeInKilobytes))
if (!IsTargetWithinFileSizeLimit(artifact.SizeInBytes.Value, globalContext.MaxFileSizeInKilobytes))
{
_filesExceedingSizeLimitCount++;
DriverEventSource.Log.ArtifactNotScanned(filePath, DriverEventNames.FileExceedsSizeLimits, artifactSize, $"{globalContext.MaxFileSizeInKilobytes}");
Notes.LogFileExceedingSizeLimitSkipped(globalContext, filePath, artifactSize / 1000);
DriverEventSource.Log.ArtifactNotScanned(filePath, DriverEventNames.FileExceedsSizeLimits, artifact.SizeInBytes.Value, $"{globalContext.MaxFileSizeInKilobytes}");
Notes.LogFileExceedingSizeLimitSkipped(globalContext, artifact.Uri.GetFilePath(), artifact.SizeInBytes.Value / 1000);
continue;
}

Expand Down
39 changes: 2 additions & 37 deletions src/Sarif/EnumeratedArtifact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.CodeAnalysis.Sarif
{
Expand All @@ -21,7 +20,6 @@ public EnumeratedArtifact(IFileSystem fileSystem)

internal byte[] bytes;
internal string contents;
internal string cleanPath = string.Empty;

private Encoding encoding;

Expand Down Expand Up @@ -66,39 +64,6 @@ public byte[] Bytes
set => this.bytes = value;
}

public string CleanPath
{
get => GetCleanFilePath(Uri.OriginalString);
set => this.cleanPath = value;
}

private readonly static Regex AbsoluteFilePathWithFileSchemeRegex = new Regex(@"file:(?:\/{3}|\/{2}\w+\/|\/{1})?(.+)", RegexOptions.Compiled | RegexOptions.CultureInvariant);

private string GetCleanFilePath(string originalString)
{
if (this.cleanPath != string.Empty)
{
return this.cleanPath;
}

if (originalString.StartsWith("file:"))
{
Match match = AbsoluteFilePathWithFileSchemeRegex.Match(originalString);
if (match.Success)
{
string separator = Path.DirectorySeparatorChar.ToString();
this.cleanPath = match.Groups[1].Value.Replace("/", separator);
return this.cleanPath;
}

throw new IOException("Invalid file path format.");
}
else
{
return originalString;
}
}

private (string text, byte[] bytes) GetArtifactData()
{
if (this.contents != null)
Expand All @@ -121,7 +86,7 @@ private string GetCleanFilePath(string originalString)
}

// This is our client-side, disk-based file retrieval case.
this.Stream = FileSystem.FileOpenRead(CleanPath);
this.Stream = FileSystem.FileOpenRead(Uri.LocalPath);
}

RetrieveDataFromStream();
Expand Down Expand Up @@ -199,7 +164,7 @@ public long? SizeInBytes
}
else if (Uri != null && Uri.IsAbsoluteUri && Uri.IsFile)
{
this.sizeInBytes = (long)FileSystem.FileInfoLength(CleanPath);
this.sizeInBytes = (long)FileSystem.FileInfoLength(Uri.LocalPath);
}
else if (this.Contents != null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sarif/Notes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void LogFileSkipped(IAnalysisContext context, string skippedFile,
Path.GetFileName(skippedFile),
reason));

context.RuntimeErrors |= RuntimeConditions.OneOrMoreFilesSkipped;
context.RuntimeErrors |= RuntimeConditions.OneOrMoreFilesSkippedDueToExceedingSizeLimits;
}

public static void LogEmptyFileSkipped(IAnalysisContext context, string skippedFile)
Expand Down

0 comments on commit a481c81

Please sign in to comment.