Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
fix #2917 and improve on tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
suwatch committed Mar 6, 2019
1 parent b5e4a6f commit 90c5f7b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
5 changes: 5 additions & 0 deletions Kudu.Contracts/Settings/DeploymentSettingsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,10 @@ public static int GetMaxZipPackageCount(this IDeploymentSettingsManager settings

return DEFAULT_ALLOWED_ZIPS;
}

public static bool GetZipDeployDoNotPreserveFileTime(this IDeploymentSettingsManager settings)
{
return "1" == settings.GetValue(SettingsKeys.ZipDeployDoNotPreserveFileTime);
}
}
}
1 change: 1 addition & 0 deletions Kudu.Contracts/Settings/SettingsKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public static class SettingsKeys
public const string RunFromZipOld = "WEBSITE_RUN_FROM_ZIP"; // Old name, will eventually go away
public const string RunFromZip = "WEBSITE_RUN_FROM_PACKAGE";
public const string MaxZipPackageCount = "SCM_MAX_ZIP_PACKAGE_COUNT";
public const string ZipDeployDoNotPreserveFileTime = "SCM_ZIPDEPLOY_DONOT_PRESERVE_FILETIME";
}
}
68 changes: 49 additions & 19 deletions Kudu.Core/Infrastructure/ZipArchiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,62 @@ public static ZipArchiveEntry AddFile(this ZipArchive zip, string fileName, stri
return entry;
}

public static void Extract(this ZipArchive archive, string directoryName)
public static void Extract(this ZipArchive archive, string directoryName, ITracer tracer, bool doNotPreserveFileTime = false)
{
foreach (ZipArchiveEntry entry in archive.Entries)
const int MaxExtractTraces = 5;

var entries = archive.Entries;
var total = entries.Count;
var traces = 0;
using (tracer.Step(string.Format("Extracting {0} entries to {1} directory", entries.Count, directoryName)))
{
string path = Path.Combine(directoryName, entry.FullName);
if (entry.Length == 0 && (path.EndsWith("/", StringComparison.Ordinal) || path.EndsWith("\\", StringComparison.Ordinal)))
{
// Extract directory
FileSystemHelpers.CreateDirectory(path);
}
else
foreach (ZipArchiveEntry entry in entries)
{
FileInfoBase fileInfo = FileSystemHelpers.FileInfoFromFileName(path);

if (!fileInfo.Directory.Exists)
string path = Path.Combine(directoryName, entry.FullName);
if (entry.Length == 0 && (path.EndsWith("/", StringComparison.Ordinal) || path.EndsWith("\\", StringComparison.Ordinal)))
{
fileInfo.Directory.Create();
// Extract directory
FileSystemHelpers.CreateDirectory(path);
}

using (Stream zipStream = entry.Open(),
fileStream = fileInfo.Open(FileMode.Create, FileAccess.Write))
else
{
zipStream.CopyTo(fileStream);
FileInfoBase fileInfo = FileSystemHelpers.FileInfoFromFileName(path);

string message = null;
// tracing first/last N files
if (traces < MaxExtractTraces || traces >= total - MaxExtractTraces)
{
message = string.Format("Extracting to {0} ...", fileInfo.FullName);
}
else if (traces == MaxExtractTraces)
{
message = "Omitting extracting file traces ...";
}

++traces;

using (!string.IsNullOrEmpty(message) ? tracer.Step(message) : null)
{
if (!fileInfo.Directory.Exists)
{
fileInfo.Directory.Create();
}

using (Stream zipStream = entry.Open(),
fileStream = fileInfo.Open(FileMode.Create, FileAccess.Write))
{
zipStream.CopyTo(fileStream);
}

// this is to allow, the file time to always be newer than destrination
// the outcome is always replacing the destination files.
// it is non-optimized but workaround NPM reset file time issue (https://github.com/projectkudu/kudu/issues/2917).
if (!doNotPreserveFileTime)
{
fileInfo.LastWriteTimeUtc = entry.LastWriteTime.ToUniversalTime().DateTime;
}
}
}

fileInfo.LastWriteTimeUtc = entry.LastWriteTime.ToUniversalTime().DateTime;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Kudu.Core/Jobs/JobsManagerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public TJob CreateOrReplaceJobFromZipStream(Stream zipStream, string jobName)
{
using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Read))
{
zipArchive.Extract(jobDirectory.FullName);
zipArchive.Extract(jobDirectory.FullName, TraceFactory.GetTracer());
}
});
}
Expand Down
7 changes: 5 additions & 2 deletions Kudu.Services/Deployment/PushDeploymentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ private async Task LocalZipFetch(IRepository repository, DeploymentInfoBase depl
if (targetInfo.Exists)
{
var moveTarget = Path.Combine(targetInfo.Parent.FullName, Path.GetRandomFileName());
targetInfo.MoveTo(moveTarget);
using (tracer.Step(string.Format("Renaming extractTargetDirectory({0}) to tempDirectory({1})", targetInfo.FullName, moveTarget)))
{
targetInfo.MoveTo(moveTarget);
}
}

var cleanTask = Task.Run(() => DeleteFilesAndDirsExcept(sourceZipFile, extractTargetDirectory, tracer));
Expand All @@ -382,7 +385,7 @@ private async Task LocalZipFetch(IRepository repository, DeploymentInfoBase depl
using (var file = info.OpenRead())
using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
{
zip.Extract(extractTargetDirectory);
zip.Extract(extractTargetDirectory, tracer, _settings.GetZipDeployDoNotPreserveFileTime());
}
});

Expand Down
2 changes: 1 addition & 1 deletion Kudu.Services/Zip/ZipController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected override async Task<HttpResponseMessage> CreateDirectoryPutResponse(Di
// Hence it's more of a PATCH than a PUT. We should consider supporting both with the right semantic.
// Though a true PUT at the root would be scary as it would wipe all existing files!
var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read);
zipArchive.Extract(localFilePath);
zipArchive.Extract(localFilePath, Tracer);
}

return Request.CreateResponse(HttpStatusCode.OK);
Expand Down

0 comments on commit 90c5f7b

Please sign in to comment.