public override void CalcHashes(Hashes hashFlags, Validations validationFlags) { if (base.Data.Issues.HasFatal) { return; } if ((validationFlags & Validations.Exists) != 0) { Data.MissingCount = 0; if (Data.Files.Items.Count != 1 || Data.Files.Items[0].Name != Data.IgnoredName) { for (int ix = 0; ix < Data.Files.Items.Count; ++ix) { FileItem item = Data.Files.Items[ix]; var name = item.Name; if (Data.AllowNonFile && (name.StartsWith("http:") || name.StartsWith("https:"))) { IssueModel.Add($"Ignoring URL '{name}'.", Severity.Trivia); } else { try { if (!System.IO.Path.IsPathRooted(name)) { name = Data.Files.RootDir + System.IO.Path.DirectorySeparatorChar + name; } else if (Data.ForbidRooted) { IssueModel.Add($"File is rooted: '{item.Name}'."); } } catch (ArgumentException ex) { IssueModel.Add($"Malformed file name '{name}': {ex.Message}"); FilesModel.SetIsFound(ix, false); ++Data.MissingCount; continue; } // Exists doesn't seem to throw any exceptions, so no try/catch. bool isFound = File.Exists(name); FilesModel.SetIsFound(ix, isFound); if (!isFound) { IssueModel.Add($"File '{item.Name}' not found.", Severity.Advisory); ++Data.MissingCount; } } } } } base.CalcHashes(hashFlags, validationFlags); }
public string RepairMissing(bool isFinalRepair) { if (Data.fBuf == null || actualFlacs == null || actualFlacs.Count == 0 || actualFlacs.Count != Data.Files.Items.Count || Data.Issues.MaxSeverity >= Severity.Error) { return("Invalid attempt"); } string err = null; int buf2Size = Data.fBuf.Length; for (int ix = 0; ix < actualFlacs.Count; ++ix) { buf2Size += actualFlacs[ix].Name.Length - Data.Files.Items[ix].Name.Length; } var buf2 = new byte[buf2Size]; int bufIx0 = Data.Files.Items[0].BufIndex; int length = bufIx0; Array.Copy(Data.fBuf, buf2, bufIx0); int dstIx = 0; for (int ix = 0;;) { dstIx += length; if (dstIx >= buf2.Length) { break; } byte[] nameBytes = FormatBase.Cp1252.GetBytes(actualFlacs[ix].Name); Array.Copy(nameBytes, 0, buf2, dstIx, nameBytes.Length); dstIx += nameBytes.Length; string try1252 = FormatBase.Cp1252.GetString(nameBytes); if (err == null && try1252 != actualFlacs[ix].Name) { err = "Track file name(s) not Windows-1252 clean."; } int srcIx = Data.Files.Items[ix].BufIndex2; ++ix; length = (ix == actualFlacs.Count ? Data.fBuf.Length : Data.Files.Items[ix].BufIndex) - srcIx; Array.Copy(Data.fBuf, srcIx, buf2, dstIx, length); } if (err == null) { try { Data.fbs.Position = bufIx0; Data.fbs.Write(buf2, bufIx0, buf2.Length - bufIx0); if (Data.fbs.Length != buf2.Length) { Data.fbs.SetLength(buf2.Length); } if (isFinalRepair) { CloseFile(); } for (int ix = 0; ix < Data.Files.Items.Count; ++ix) { FilesModel.SetIsFound(ix, true); FilesModel.SetName(ix, actualFlacs[ix].Name); } } catch (Exception ex) when(ex is UnauthorizedAccessException || ex is IOException) { err = ex.Message.TrimEnd(null); } } return(err); }
public override void CalcHashes(Hashes hashFlags, Validations validationFlags) { if (base.Data.Issues.HasFatal) { return; } if ((validationFlags & Validations.Exists) != 0) { if (Data.Files.Items.Count != 1 || Data.Files.Items[0].Name != Data.IgnoredName) { int notFoundTotal = 0; for (int ix = 0; ix < Data.Files.Items.Count; ++ix) { FileItem item = Data.Files.Items[ix]; var name = item.Name; if (Data.AllowNonFile && (name.StartsWith("http:") || name.StartsWith("https:"))) { IssueModel.Add("Ignoring URL '" + name + "'.", Severity.Trivia); } else { try { if (!System.IO.Path.IsPathRooted(name)) { name = Data.Files.RootDir + System.IO.Path.DirectorySeparatorChar + name; } else if (Data.ForbidRooted) { IssueModel.Add("File is rooted: '" + item.Name + "'."); } } catch (ArgumentException ex) { IssueModel.Add($"Malformed file name '{name}': {ex.Message}"); FilesModel.SetIsFound(ix, false); ++notFoundTotal; continue; } // Exists doesn't seem to throw any exceptions, so no try/catch. bool isFound = File.Exists(name); FilesModel.SetIsFound(ix, isFound); if (!isFound) { IssueModel.Add("Missing file '" + item.Name + "'."); ++notFoundTotal; } } } var sfx = Data.Files.Items.Count == 1? String.Empty : "s"; var tx = "Existence check" + sfx + " of " + Data.Files.Items.Count + " file" + sfx; if (notFoundTotal == 0) { tx += " successful."; } else { tx += " failed with " + notFoundTotal + " not found."; } IssueModel.Add(tx, notFoundTotal == 0? Severity.Advisory : Severity.Error); } } base.CalcHashes(hashFlags, validationFlags); }