Skip to content

Commit

Permalink
Revert "Merge pull request praeclarum#152 from MarkPflug/cache_local"
Browse files Browse the repository at this point in the history
This reverts commit 64e23bf, reversing
changes made to 971a83c.
  • Loading branch information
praeclarum committed Mar 16, 2022
1 parent 64e23bf commit 9c613d3
Showing 1 changed file with 50 additions and 141 deletions.
191 changes: 50 additions & 141 deletions Data/PackageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class Entry
public int ExpandedSize { get; set; }
public int EntryOffset { get; set; }
public int Length => ExpandedSize;

public override string ToString ()
{
return $"{FullName} {Mode}";
Expand All @@ -35,27 +34,6 @@ public Stream Open ()
}

public async Task<Stream> OpenAsync () {

var cacheDir =
Path.Combine (
Path.GetTempPath (),
"FugetFileCache",
this.Package.Id.ToLowerInvariant (), // might run on linux?
this.Package.Version.ToString ().ToLowerInvariant (),
Path.GetDirectoryName (this.FullName)
);
Directory.CreateDirectory (cacheDir);
var cacheFile = Path.Combine (cacheDir, this.Name);

if (File.Exists (cacheFile)) {
try {
var cachedFileStream = File.OpenRead (cacheFile);
Debug.WriteLine ("Using cached file: " + cacheFile);
return cachedFileStream;
}
catch {
}
}

Debug.WriteLine ("Downloading: " + this.FullName + " " + this.CompressedSize);
var url = this.Package.DownloadUrl;
Expand All @@ -69,8 +47,8 @@ public async Task<Stream> OpenAsync () {
PackageData.AddDataRequestHeaders(req);
req.Headers.Add ("Range", "bytes=" + EntryOffset + "-" + (EntryOffset + 30 -1));

var resp = await client.SendAsync (req).ConfigureAwait(false);
var buf = await resp.Content.ReadAsByteArrayAsync ().ConfigureAwait (false);
var resp = await client.SendAsync (req);
var buf = await resp.Content.ReadAsByteArrayAsync ();

var fileNameLen = buf.GetInt16 (26);
var fileExtraLen = buf.GetInt16 (28);
Expand All @@ -86,10 +64,11 @@ public async Task<Stream> OpenAsync () {

var deflateStart = EntryOffset + 30 + fileNameLen + fileExtraLen;


req.Headers.Add ("Range", "bytes=" + deflateStart + "-" + (deflateStart + CompressedSize - 1));

resp = await client.SendAsync (req).ConfigureAwait (false);
Stream stream = await resp.Content.ReadAsStreamAsync ().ConfigureAwait (false);
resp = await client.SendAsync (req);
Stream stream = await resp.Content.ReadAsStreamAsync ();
if (Mode == 8) // deflate
{
stream = new DeflateStream (stream, CompressionMode.Decompress, false);
Expand All @@ -102,28 +81,6 @@ public async Task<Stream> OpenAsync () {

stream.CopyTo (ms);
ms.Position = 0;
var tempFile = Path.GetTempFileName ();
try {

using (var cacheFileStream = new FileStream (tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 0x1000, true)) {
await ms.CopyToAsync (cacheFileStream).ConfigureAwait (false);
}
File.Move (tempFile, cacheFile);
}
catch {
Debug.WriteLine ("Exception writing cached file: " + cacheFile);
try {
File.Delete (cacheFile);
}
catch { }
}
try {
// this should usually fail, since the file should have been moved.
File.Delete (tempFile);
}
catch { }
ms.Position = 0;

stream.Dispose ();
return ms;
}
Expand Down Expand Up @@ -254,108 +211,59 @@ public static void AddDataRequestHeaders(HttpRequestMessage req)
req.Headers.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("(+https://github.com/praeclarum/FuGetGallery)"));
}

async Task<List<Entry>> ReadEntriesAsync (HttpClient client)
async Task<List<Entry>> ReadEntriesAsync (HttpClient client, string file)
{
var tocCacheFile =
Path.Combine (
Path.GetTempPath (),
"FugetFileCache",
this.Id.ToLowerInvariant (),
this.Version.ToString().ToLowerInvariant() + ".toc"
);

byte[] buf;
int entryCount;
int offset = 0;

if (File.Exists (tocCacheFile))
{
buf = await File.ReadAllBytesAsync (tocCacheFile);
entryCount = BitConverter.ToInt32 (buf, 0);
offset = 4;
}
else
{
var file = this.DownloadUrl;

var req = new HttpRequestMessage () {
Method = HttpMethod.Get,
RequestUri = new Uri (file, UriKind.Absolute),
Version = new Version (1, 1),
};
AddDataRequestHeaders (req);
req.Headers.Add ("Range", "bytes=0-1");
var resp = await client.SendAsync (req);
buf = await resp.Content.ReadAsByteArrayAsync ();
var str = resp.Content.Headers.GetValues ("Content-Range").Single ();
var m = ContentRangeRegex.Match (str);
var len = int.Parse (m.Groups[3].Value);

req = new HttpRequestMessage () {
Method = HttpMethod.Get,
RequestUri = new Uri (file, UriKind.Absolute),
Version = new Version (1, 1),
};
AddDataRequestHeaders (req);

// this only works if the zip doesn't have a comment. which I think, should always be true, but who knows.
req.Headers.Add ("Range", "bytes=" + (len - 22) + "-" + (len - 1));

resp = await client.SendAsync (req);
var req = new HttpRequestMessage () {
Method = HttpMethod.Get,
RequestUri = new Uri (file, UriKind.Absolute),
Version = new Version (1, 1),
};
AddDataRequestHeaders(req);
req.Headers.Add ("Range", "bytes=0-1");
var resp = await client.SendAsync (req);
var buf = await resp.Content.ReadAsByteArrayAsync ();
var str = resp.Content.Headers.GetValues ("Content-Range").Single ();
var m = ContentRangeRegex.Match (str);
var len = int.Parse (m.Groups[3].Value);

buf = await resp.Content.ReadAsByteArrayAsync ();
req = new HttpRequestMessage () {
Method = HttpMethod.Get,
RequestUri = new Uri (file, UriKind.Absolute),
Version = new Version (1, 1),
};
AddDataRequestHeaders(req);

var sig = buf.GetInt32 (0);
if (sig != 0x06054b50) // likely because there was a comment
throw new Exception ("Package format doesn't support quick access");
// this only works if the zip doesn't have a comment. which I think, should always be true, but who knows.
req.Headers.Add ("Range", "bytes=" + (len - 22) + "-" + (len - 1));

entryCount = buf.GetInt16 (8);
int centralDirLen = buf.GetInt32 (12);
int centralDirOff = buf.GetInt32 (16);
resp = await client.SendAsync (req);

req = new HttpRequestMessage () {
Method = HttpMethod.Get,
RequestUri = new Uri (file, UriKind.Absolute),
Version = new Version (1, 1),
};
AddDataRequestHeaders (req);
buf = await resp.Content.ReadAsByteArrayAsync ();

req.Headers.Add ("Range", "bytes=" + (centralDirOff) + "-" + (centralDirOff + centralDirLen - 1));
var sig = buf.GetInt32 (0);
if (sig != 0x06054b50) // likely because there was a comment
throw new Exception ("Package format doesn't support quick access");

resp = await client.SendAsync (req);
buf = await resp.Content.ReadAsByteArrayAsync ();
int entryCount = buf.GetInt16 (8);
int centralDirLen = buf.GetInt32 (12);
int centralDirOff = buf.GetInt32 (16);

var tempFile = Path.GetTempFileName ();
try {
req = new HttpRequestMessage () {
Method = HttpMethod.Get,
RequestUri = new Uri (file, UriKind.Absolute),
Version = new Version (1, 1),
};
AddDataRequestHeaders(req);

using (var cacheFileStream = new FileStream (tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 0x1000, true))
{
byte[] b = new byte[4];
BitConverter.TryWriteBytes (b, entryCount);
await cacheFileStream.WriteAsync (b, 0, 4);
await cacheFileStream.WriteAsync (buf, 0, buf.Length);
}
Directory.CreateDirectory (Path.GetDirectoryName (tocCacheFile));
File.Move (tempFile, tocCacheFile);
}
catch {
Debug.WriteLine ("Exception writing cached file: " + tocCacheFile);
try {
File.Delete (tocCacheFile);
}
catch { }
}
try {
File.Delete (tempFile);
}
catch { }
}
req.Headers.Add ("Range", "bytes=" + (centralDirOff) + "-" + (centralDirOff + centralDirLen - 1));

resp = await client.SendAsync (req);
buf = await resp.Content.ReadAsByteArrayAsync ();

var entries = new List<Entry> (entryCount);

var offset = 0;
for (int i = 0; i < entryCount; i++) {
var sig = buf.GetInt32 (offset + 0);
sig = buf.GetInt32 (offset + 0);
if (sig != 0x02014b50)
throw new Exception ();
var compressionMethod = buf.GetInt16 (offset + 10);
Expand Down Expand Up @@ -389,10 +297,10 @@ async Task<List<Entry>> ReadEntriesAsync (HttpClient client)
return entries;
}

async Task ReadAsync (HttpClient httpClient)
async Task ReadAsync (HttpClient httpClient, string packageUri)
{
this.Client = httpClient;
var entries = await ReadEntriesAsync (httpClient);
var entries = await ReadEntriesAsync (httpClient, packageUri);

TargetFrameworks.Clear ();
Content.Clear();
Expand Down Expand Up @@ -469,7 +377,8 @@ void ReadNuspec (Entry entry)
throw new Exception ("Failed to find metadata in " + xdoc);
}
string GetS (string name, string def = "") {
return meta.Element(ns + name)?.Value.Trim() ?? def;
try { return meta.Element(ns + name).Value.Trim(); }
catch { return def; }
}
string GetUrl (string name) {
var u = GetS (name);
Expand Down Expand Up @@ -684,7 +593,7 @@ protected override async Task<PackageData> GetValueAsync(string arg0, PackageVer

private static async Task<PackageData> ReadPackageFromUrl (PackageData package, HttpClient httpClient, CancellationToken token)
{
await package.ReadAsync (httpClient);
await package.ReadAsync (httpClient, package.DownloadUrl);
await package.MatchLicenseAsync (httpClient).ConfigureAwait (false);
await package.SaveDependenciesAsync ();
return package;
Expand Down

0 comments on commit 9c613d3

Please sign in to comment.