Skip to content

Commit

Permalink
(#357) Ensure to use full file path
Browse files Browse the repository at this point in the history
When creating an output export file from a path like "packages.config"
we need to ensure to use the full file path to the file, otherwise it
won't be able to backup the file correctly, if it already exists on the
file system.

Without this, we were seeing errors when trying to ensure that the
directory that the file was contained in was created, since this was an
empty string.
  • Loading branch information
gep13 committed May 13, 2021
1 parent 470baf7 commit 519fcc2
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ public void run(ChocolateyConfiguration configuration)
xw.Flush();
}

var fileExists = _fileSystem.file_exists(configuration.ExportCommand.OutputFilePath);
var fullOutputFilePath = _fileSystem.get_full_path(configuration.ExportCommand.OutputFilePath);
var fileExists = _fileSystem.file_exists(fullOutputFilePath);

// If the file doesn't already exist, just write the new one out directly
if (!fileExists)
{
_fileSystem.write_file(
configuration.ExportCommand.OutputFilePath,
fullOutputFilePath,
stringWriter.GetStringBuilder().ToString(),
new UTF8Encoding(false));

Expand All @@ -181,12 +182,12 @@ public void run(ChocolateyConfiguration configuration)


// Otherwise, create an update file, and resiliently move it into place.
var tempUpdateFile = configuration.ExportCommand.OutputFilePath + "." + Process.GetCurrentProcess().Id + ".update";
var tempUpdateFile = fullOutputFilePath + "." + Process.GetCurrentProcess().Id + ".update";
_fileSystem.write_file(tempUpdateFile,
stringWriter.GetStringBuilder().ToString(),
new UTF8Encoding(false));

_fileSystem.replace_file(tempUpdateFile, configuration.ExportCommand.OutputFilePath, configuration.ExportCommand.OutputFilePath + ".backup");
_fileSystem.replace_file(tempUpdateFile, fullOutputFilePath, fullOutputFilePath + ".backup");
}
},
errorMessage: "Error exporting currently installed packages",
Expand Down

0 comments on commit 519fcc2

Please sign in to comment.