mirror of
https://github.com/github/codeql.git
synced 2026-04-28 02:05:14 +02:00
Improve source archiving to handle non-existent files
This commit is contained in:
@@ -115,19 +115,6 @@ namespace Semmle.Extraction
|
||||
ArchivePath(fullInputPath, transformedPath, inputEncoding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Archive a file given the file contents.
|
||||
/// </summary>
|
||||
/// <param name="inputPath">The path of the file.</param>
|
||||
/// <param name="contents">The contents of the file.</param>
|
||||
public void Archive(PathTransformer.ITransformedPath inputPath, string contents)
|
||||
{
|
||||
if (string.IsNullOrEmpty(archive))
|
||||
return;
|
||||
|
||||
ArchiveContents(inputPath, contents);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to move a file from sourceFile to destFile.
|
||||
/// If successful returns true,
|
||||
@@ -209,23 +196,21 @@ namespace Semmle.Extraction
|
||||
/// <exception cref="PathTooLongException">If the output path in the source archive would
|
||||
/// exceed the system path limit of 260 characters.</exception>
|
||||
private void ArchivePath(string fullInputPath, PathTransformer.ITransformedPath transformedPath, Encoding inputEncoding)
|
||||
{
|
||||
var contents = File.ReadAllText(fullInputPath, inputEncoding);
|
||||
ArchiveContents(transformedPath, contents);
|
||||
}
|
||||
|
||||
private void ArchiveContents(PathTransformer.ITransformedPath transformedPath, string contents)
|
||||
{
|
||||
var dest = FileUtils.NestPaths(logger, archive, transformedPath.Value);
|
||||
var tmpSrcFile = Path.GetTempFileName();
|
||||
File.WriteAllText(tmpSrcFile, contents, utf8);
|
||||
try
|
||||
{
|
||||
var contents = File.ReadAllText(fullInputPath, inputEncoding);
|
||||
var tmpSrcFile = Path.GetTempFileName();
|
||||
File.WriteAllText(tmpSrcFile, contents, utf8);
|
||||
|
||||
FileUtils.MoveOrReplace(tmpSrcFile, dest);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// If this happened, it was probably because the same file was compiled multiple times.
|
||||
// If this happened, it was probably because
|
||||
// - the same file was compiled multiple times, or
|
||||
// - the file doesn't exist (due to wrong #line directive or because it's an in-memory source generated AST).
|
||||
// In any case, this is not a fatal error.
|
||||
logger.LogWarning("Problem archiving " + dest + ": " + ex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user