/// <summary> /// Add folder and it´s subfolders. /// </summary> /// <param name="folder">Path to folder to add.</param> /// <param name="progressInfo">Returns information about progress.</param> void AddSubFolders(string folder, ref ProgressInfo progressInfo) { AddFolder(folder, ref progressInfo); string[] subFolders = Directory.GetDirectories(folder); for (int i = 0; i < subFolders.Length; i++) { string currentSubFolder = subFolders[i]; try { if (Directory.GetDirectories(currentSubFolder).Length > 0) { AddSubFolders(currentSubFolder, ref progressInfo); } else { AddFolder(currentSubFolder, ref progressInfo); } } catch (Exception) { //TODO: Tell user he did not get all subfolders. //Some windows hidden folders throw this exception. } if (progressInfo.IsStopping()) { progressInfo.ActivityEnded(); break; } } }
/// <summary> /// Calculate checksums. /// </summary> /// <param name="itemList">List of items for which to calculate.</param> /// <param name="progressInfo">Returns information about calculation /// progress.</param> public void Calculate(List <CheckSumItem> itemList, ref ProgressInfo progressInfo) { ICheckSum sumImpl = CheckSumImplList.GetImplementation(_sumType); int i = 0; foreach (CheckSumItem ci in itemList) { progressInfo.Filename = ci.FullPath; // Check if fhe file is found and accessible try { FileInfo fi = new FileInfo(ci.FullPath); if (!fi.Exists) { continue; } } catch { // Ignore non-existing and non-accessible files // TODO: Should we inform user? continue; } try { using (FileStream filestream = new FileStream(ci.FullPath, FileMode.Open, FileAccess.Read)) { byte[] hash = sumImpl.Calculate(filestream); filestream.Close(); ci.SetSum(hash); } } catch (IOException) { //TODO: Set failure status to checksum item. } // Update progress information after item completed i++; progressInfo.Now = i; if (progressInfo.IsStopping()) { progressInfo.ActivityEnded(); break; } } }
/// <summary> /// Verify checksums. /// </summary> /// <param name="itemList">List of items to verify.</param> /// <param name="progressInfo">Returns information about calculation /// progress.</param> /// <returns> /// true if all items succeeded the verification, false if /// one or more items failed verification. /// </returns> public bool Verify(List <CheckSumItem> itemList, ref ProgressInfo progressInfo) { bool verifysucceeded = true; ICheckSum sumImpl = CheckSumImplList.GetImplementation(_sumType); int i = 0; foreach (CheckSumItem ci in itemList) { progressInfo.Filename = ci.FullPath; // If verification finds an item which does not have checksum // (not calculated yet?) just ignore the item. Or maybe we // should calculate checksum first? if (ci.CheckSum == null) { verifysucceeded = false; continue; } // Check if fhe file is found and accessible try { FileInfo fi = new FileInfo(ci.FullPath); if (!fi.Exists) { verifysucceeded = false; continue; } } catch { // Ignore non-existing and non-accessible files // TODO: Should we inform user? verifysucceeded = false; continue; } bool verified = false; try { using (FileStream filestream = new FileStream(ci.FullPath, FileMode.Open, FileAccess.Read)) { verified = sumImpl.Verify(filestream, ci.CheckSum.GetAsByteArray()); filestream.Close(); } } catch (IOException) { // TODO: Set failure status to checksum item. // Let verification just fail when not implemented // TODO: Need to inform the user, but how? // For debug builds just throw the exception for reminder.. #if DEBUG throw; #endif } if (verified) { ci.Verified = VerificationState.VerifyOK; } else { ci.Verified = VerificationState.VerifyFailed; if (verifysucceeded == true) { // Set to false when we find first failed item verifysucceeded = false; } } // Update progress information after item completed i++; progressInfo.Now = i; if (progressInfo.IsStopping()) { progressInfo.ActivityEnded(); break; } } return(verifysucceeded); }