コード例 #1
0
ファイル: CheckRepo.cs プロジェクト: tsvx/CheckRepo
 static bool CheckAndUpdate(string url, string dir, RepoFileInfo f, bool checkHash)
 {
     string msg = null;
     var r = CheckFile(dir, f, ref msg, checkHash);
     if (r == CheckResult.OK)
         return true;
     if (r != CheckResult.NotExist || url == null)
         Console.WriteLine(msg);
     if (url == null)
         return false;
     Console.WriteLine("Downloading file '{0}' ...", f.Name);
     if (r != CheckResult.NotExist)
         File.Delete(dir + f.Name);
     if (!UpdateFile(url, dir, f.Name))
     {
         Console.WriteLine("Error");
         return false;
     }
     r = CheckFile(dir, f, ref msg, checkHash);
     if (r != CheckResult.OK)
     {
         Console.WriteLine(msg);
         return false;
     }
     return true;
 }
コード例 #2
0
ファイル: CheckRepo.cs プロジェクト: tsvx/CheckRepo
 static CheckResult CheckFile(string dir, RepoFileInfo f, ref string msg, bool checkHash)
 {
     string fname = dir + f.Name;
     var fi = new FileInfo(fname);
     if (!fi.Exists)
     {
         msg = String.Format("File '{0}' does not exist.", f.Name);
         return CheckResult.NotExist;
     }
     if (f.Size.HasValue && fi.Length != f.Size)
     {
         msg = String.Format("File '{0}' size {1} mismatches, needs {2}.", f.Name, fi.Length, f.Size);
         return CheckResult.BadSize;
     }
     if (checkHash)
     {
         string hType = f.CsumType.ToUpper();
         HashAlgorithm hAlg;
         if (!algs.TryGetValue(hType, out hAlg))
         {
             if (hType == "SHA256")
                 hAlg = new SHA256Cng();
             else
                 hAlg = HashAlgorithm.Create(hType);
             algs[hType] = hAlg;
         }
         string hash;
         using (var fs = File.OpenRead(fname))
             hash = String.Join("", hAlg.ComputeHash(fs).Select(b => b.ToString("x2")));
         if (f.Csum.ToLower() != hash)
         {
             msg = String.Format("File '{0}' {1}-hash {2} mismatches needed {3}.", f.Name, f.CsumType, hash, f.Csum);
             return CheckResult.BadHash;
         }
     }
     return CheckResult.OK;
 }