Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NuGet pack "The DateTimeOffset specified cannot be converted into a Zip file timestamp" #3793

Merged
merged 25 commits into from Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1e0437f
Fix NuGet pack "The DateTimeOffset specified cannot be converted into…
erdembayar Dec 10, 2020
57bb207
Add unit test for Zip file date before 1980 case
erdembayar Dec 10, 2020
e45b889
Address PR review comment by Damon and Nikolche
erdembayar Dec 11, 2020
d7bdb3c
Log that zip file modified dates are set to 1/4/1980 if it's before y…
erdembayar Dec 14, 2020
18e3bf0
Fix failing unit test
erdembayar Dec 14, 2020
3f11fc4
Make modified datetime passed to ZipArchive UTC so any file modified …
erdembayar Dec 16, 2020
5c9d833
Fix typos.
erdembayar Dec 16, 2020
ede2569
Added Zip file 2 second precision limitation comment.
erdembayar Dec 16, 2020
d89fcfb
Merge branch 'dev' into dev-eryondon-CannotBeConverted2ZipfileTimestamp
erdembayar Jan 12, 2021
b212126
Address code review comments by Andy for deterministic packing.
erdembayar Jan 13, 2021
146d4e5
Change to local Datetime for timestamping.
erdembayar Jan 15, 2021
7442f7b
Clean up
erdembayar Jan 29, 2021
dea0eef
Revert accidental changes.
erdembayar Jan 29, 2021
bd1c76a
Cleanup comments.
erdembayar Jan 29, 2021
d9e5479
Address Nikolche's code review.
erdembayar Jan 30, 2021
f24aff3
Address PR comment by Andy.
erdembayar Feb 1, 2021
af38361
Merge branch 'dev' into dev-eryondon-CannotBeConverted2ZipfileTimestamp
erdembayar Feb 1, 2021
69161ae
Address comment about string.Fomatting.
erdembayar Feb 1, 2021
a7f53aa
Fix typo.
erdembayar Feb 1, 2021
c5c3ebf
Fix typo.
erdembayar Feb 1, 2021
49cc7eb
Address more comments for string resouces.
erdembayar Feb 1, 2021
5d812dc
Address additional review comments.
erdembayar Feb 2, 2021
b7737cb
Address string.Format with string.Concat(+) for better performance.
erdembayar Feb 2, 2021
fa51fa0
Address comment by Nikolche.
erdembayar Feb 3, 2021
9bad83e
Check if warningMessage has something before trimming new line.
erdembayar Feb 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,15 +10,13 @@
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Xml.Linq;
using NuGet.Client;
using NuGet.Common;
using NuGet.ContentModel;
using NuGet.Frameworks;
using NuGet.Packaging.Core;
using NuGet.Packaging.PackageCreation.Resources;
using NuGet.Packaging.Rules;
using NuGet.RuntimeModel;
using NuGet.Versioning;

Expand Down Expand Up @@ -893,7 +891,7 @@ private ZipArchiveEntry CreateEntry(ZipArchive package, string entryName, Compre
private ZipArchiveEntry CreatePackageFileEntry(ZipArchive package, string entryName, DateTimeOffset timeOffset, CompressionLevel compressionLevel)
{
var entry = package.CreateEntry(entryName, compressionLevel);
entry.LastWriteTime = timeOffset;
entry.LastWriteTime = timeOffset < ZipFormatMinDate ? ZipFormatMinDate : timeOffset;
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
return entry;
}

Expand Down
Expand Up @@ -2857,6 +2857,62 @@ public void PackageBuilderPreserveFileLastWriteTime()
}
}

[Fact]
public void PackageBuilderCorrectLastWriteTimeForZipfileBefore1980()
{
// https://github.com/NuGet/Home/issues/7001
zivkan marked this conversation as resolved.
Show resolved Hide resolved
// Act
DateTime ZipFormatMinDate = new DateTime(1980, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var lastWriteTime = new DateTimeOffset(1979, 11, 15, 23, 59, 0, TimeSpan.Zero);
int numberOfDateCorrectedFiles = 0;
int numberOfDateNotCorrectedFiles = 0;

using (var directory = new TestLastWriteTimeDirectory(lastWriteTime))
{
var builder = new PackageBuilder { Id = "test", Version = NuGetVersion.Parse("1.0"), Description = "test" };
builder.Authors.Add("test");

// Create a file that is modified after 1980 and it shouldn't be modified.
string after1980File = Path.Combine(directory.Path, "After1980.txt");
File.WriteAllText(after1980File, string.Empty);
File.SetLastWriteTime(after1980File, ZipFormatMinDate.AddMinutes(1));

builder.AddFiles(directory.Path, "**", "Content");

using (var stream = new MemoryStream())
{
builder.Save(stream);

// Assert
using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true))
{
foreach (var entry in archive.Entries)
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
{
var path = directory.Path + Path.DirectorySeparatorChar + entry.Name;
erdembayar marked this conversation as resolved.
Show resolved Hide resolved
// Only checks the entries that originated from files in test directory
if (File.Exists(path))
{
if (path == after1980File)
{
Assert.Equal(entry.LastWriteTime.DateTime, ZipFormatMinDate.AddMinutes(1));
numberOfDateNotCorrectedFiles++;
}
else
{
Assert.NotEqual(entry.LastWriteTime.DateTime, File.GetLastWriteTimeUtc(path));
Assert.Equal(entry.LastWriteTime.DateTime, ZipFormatMinDate);
numberOfDateCorrectedFiles++;
}
}
}
}
}

Assert.True(numberOfDateNotCorrectedFiles == 1);
Assert.True(numberOfDateCorrectedFiles > 0);
}
}

private static IPackageFile CreatePackageFile(string name)
{
var file = new Mock<IPackageFile>();
Expand Down