Improve source archiving to handle non-existent files

This commit is contained in:
Tamas Vajk
2024-06-13 16:31:01 +02:00
parent 31ad195dc3
commit dcd84f47a4

View File

@@ -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);
}