private static Func <FtpListItem, FileInfo, bool> GetVersionEqualsComparer(UpdateInfo updateInfo, FtpListItem updateRoot) { switch (updateInfo.Versioning) { case UpdateInfo.VersioningMode.Size: return((item, info) => item.Size == info.Length); case UpdateInfo.VersioningMode.Date: return((item, info) => GetDate(item) <= info.LastWriteTimeUtc); case UpdateInfo.VersioningMode.Contents: if (!updateInfo.ContentHashes.Any()) { Console.WriteLine("No hashes found in " + updateInfo.GUID + " while VersioningMode was set to Contents, falling back to Size"); goto case UpdateInfo.VersioningMode.Size; } var rootLength = updateRoot.FullName.Length; return((item, info) => { var match = updateInfo.ContentHashes.FirstOrDefault(x => PathTools.PathsEqual(x.RelativeFileName, item.FullName.Substring(rootLength))); if (match == null || match.Hash == 0) { Console.WriteLine($"No hash found on remote for file {item.FullName.Substring(rootLength)} - comparing size instead"); return item.Size == info.Length; } return FileContentsCalculator.GetFileHash(info) == match.Hash; }); default: throw new ArgumentOutOfRangeException(); } }
private static Func <IRemoteItem, FileInfo, bool> GetVersionEqualsComparer(UpdateInfo updateInfo) { switch (updateInfo.Versioning) { case UpdateInfo.VersioningMode.Size: return((item, info) => item.ItemSize == info.Length); case UpdateInfo.VersioningMode.Date: return((item, info) => item.ModifiedTime <= info.LastWriteTimeUtc); case UpdateInfo.VersioningMode.Contents: if (!updateInfo.ContentHashes.Any()) { Console.WriteLine($"No hashes found in {updateInfo.GUID} while VersioningMode was set to Contents, falling back to Size"); goto case UpdateInfo.VersioningMode.Size; } return((remote, local) => { var h = FileContentsCalculator.GetFileHash(local); var match = updateInfo.ContentHashes.FirstOrDefault(hash => PathTools.PathsEqual(hash.RelativeFileName, remote.ClientRelativeFileName)); if (match != null && h != null) { if (h.SB3UHash != 0 && match.SB3UHash != 0) { return h.SB3UHash == match.SB3UHash; } if (h.FileHash != 0 && match.FileHash != 0) { return h.FileHash == match.FileHash; } } Console.WriteLine($"No hash found on remote for file {remote.ClientRelativeFileName}, comparing file size instead"); return remote.ItemSize == local.Length; }); default: throw new ArgumentOutOfRangeException(); } }