Include and validate checksums with locally cached manifests

pull/67/head
Ryan Kistner 6 years ago
parent 0b598b43a7
commit 2e30364d8f

@ -583,8 +583,30 @@ namespace DepotDownloader
if ( lastManifestId != INVALID_MANIFEST_ID ) if ( lastManifestId != INVALID_MANIFEST_ID )
{ {
var oldManifestFileName = Path.Combine( configDir, string.Format( "{0}.bin", lastManifestId ) ); var oldManifestFileName = Path.Combine( configDir, string.Format( "{0}.bin", lastManifestId ) );
if ( File.Exists( oldManifestFileName ) )
oldProtoManifest = ProtoManifest.LoadFromFile( oldManifestFileName ); if (File.Exists(oldManifestFileName))
{
byte[] expectedChecksum, currentChecksum;
try
{
expectedChecksum = File.ReadAllBytes(oldManifestFileName + ".sha");
}
catch (IOException)
{
expectedChecksum = null;
}
oldProtoManifest = ProtoManifest.LoadFromFile(oldManifestFileName, out currentChecksum);
if (expectedChecksum == null || !expectedChecksum.SequenceEqual(currentChecksum))
{
// We only have to show this warning if the old manifest ID was different
if (lastManifestId != depot.manifestId)
Console.WriteLine("Manifest {0} on disk did not match the expected checksum.", lastManifestId);
oldProtoManifest = null;
}
}
} }
if ( lastManifestId == depot.manifestId && oldProtoManifest != null ) if ( lastManifestId == depot.manifestId && oldProtoManifest != null )
@ -597,7 +619,24 @@ namespace DepotDownloader
var newManifestFileName = Path.Combine( configDir, string.Format( "{0}.bin", depot.manifestId ) ); var newManifestFileName = Path.Combine( configDir, string.Format( "{0}.bin", depot.manifestId ) );
if ( newManifestFileName != null ) if ( newManifestFileName != null )
{ {
newProtoManifest = ProtoManifest.LoadFromFile( newManifestFileName ); byte[] expectedChecksum, currentChecksum;
try
{
expectedChecksum = File.ReadAllBytes(newManifestFileName + ".sha");
}
catch (IOException)
{
expectedChecksum = null;
}
newProtoManifest = ProtoManifest.LoadFromFile(newManifestFileName, out currentChecksum);
if (expectedChecksum == null || !expectedChecksum.SequenceEqual(currentChecksum))
{
Console.WriteLine("Manifest {0} on disk did not match the expected checksum.", depot.manifestId);
newProtoManifest = null;
}
} }
if ( newProtoManifest != null ) if ( newProtoManifest != null )
@ -656,8 +695,11 @@ namespace DepotDownloader
return; return;
} }
byte[] checksum;
newProtoManifest = new ProtoManifest( depotManifest, depot.manifestId ); newProtoManifest = new ProtoManifest( depotManifest, depot.manifestId );
newProtoManifest.SaveToFile( newManifestFileName ); newProtoManifest.SaveToFile( newManifestFileName, out checksum );
File.WriteAllBytes( newManifestFileName + ".sha", checksum );
Console.WriteLine( " Done!" ); Console.WriteLine( " Done!" );
} }

@ -117,21 +117,42 @@ namespace DepotDownloader
[ProtoMember(2)] [ProtoMember(2)]
public ulong ID { get; private set; } public ulong ID { get; private set; }
public static ProtoManifest LoadFromFile(string filename) public static ProtoManifest LoadFromFile(string filename, out byte[] checksum)
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{
checksum = null;
return null; return null;
}
using (MemoryStream ms = new MemoryStream())
{
using (FileStream fs = File.Open(filename, FileMode.Open))
using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Decompress))
ds.CopyTo(ms);
checksum = Util.SHAHash(ms.ToArray());
using (FileStream fs = File.Open(filename, FileMode.Open)) ms.Seek(0, SeekOrigin.Begin);
using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Decompress)) return ProtoBuf.Serializer.Deserialize<ProtoManifest>(ms);
return ProtoBuf.Serializer.Deserialize<ProtoManifest>(ds); }
} }
public void SaveToFile(string filename) public void SaveToFile(string filename, out byte[] checksum)
{ {
using (FileStream fs = File.Open(filename, FileMode.Create))
using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Compress)) using (MemoryStream ms = new MemoryStream())
ProtoBuf.Serializer.Serialize<ProtoManifest>(ds, this); {
ProtoBuf.Serializer.Serialize<ProtoManifest>(ms, this);
checksum = Util.SHAHash(ms.ToArray());
ms.Seek(0, SeekOrigin.Begin);
using (FileStream fs = File.Open(filename, FileMode.Create))
using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Compress))
ms.CopyTo(ds);
}
} }
} }
} }

Loading…
Cancel
Save