bool tryRemoveDupe(FileDetail fdMstr, FileDetail fdDupe) { var masterFullPathName = Path.Combine(fdMstr.FilePath, fdMstr.FileName); var doubleFullPathName = Path.Combine(fdDupe.FilePath, fdDupe.FileName); if (string.Compare(masterFullPathName, doubleFullPathName, true) == 0) { if (Debugger.IsAttached) { Debugger.Break(); } return(false); } // only if not the same file if (File.Exists(masterFullPathName) && File.Exists(doubleFullPathName)) // redundant but still... { if (string.Compare(fdMstr.FileHash, fdDupe.FileHash) == 0) { if (fdMstr.FileSize != fdDupe.FileSize) { throw new Exception("File sizes differ!!!"); } safeDeleteFile(fdDupe, masterFullPathName, doubleFullPathName, "<Deleting full (hash+size) match of {0}.>\r\n", fdDupe.FileSize); return(true); } else { var nu = new DelPopup(); _newUenWindowF.ShowNewWindow(nu, doubleFullPathName, masterFullPathName, fdDupe.FileHash, fdMstr.FileHash, fdDupe.FileSize, fdMstr.FileSize); if (nu.OkToDelete == null) { Trace.WriteLine("Close/Exit requested......."); return(false); } else if (nu.OkToDelete.Value) { safeDeleteFile(fdDupe, masterFullPathName, doubleFullPathName, "<Deleting manually verified match of {0}>\r\n", fdDupe.FileSize); return(true); } else { smartUpdateNote(fdDupe, $"{_ndm} {masterFullPathName}>\r\n"); } } } else { smartUpdateNote(fdDupe, string.Format(_ndsne, masterFullPathName)); } return(true); }
async Task <long> makeSureItIsDupeNadSafeDelete(FileInfo delCandidate, string mainFolder, int mainRootLen, string dupeFolder, int dupeRootLen, A0DbContext db, Stopwatch sw, SHA256 ha, long ttlLen, long prgLen) { prgLen = await updateMsgInfo(sw, ttlLen, prgLen, delCandidate, "F2"); var superPath = Path.GetFullPath(delCandidate.FullName).Substring(mainRootLen); // only works within OneDrive. var oneDrBase = Path.GetFullPath(mainFolder).Substring(0, mainRootLen); FileDetail dupeInDb = null; lock (_thisLock) { dupeInDb = db.FileDetails.FirstOrDefault(r => r.FileSize == delCandidate.Length && string.Compare(r.FileName, delCandidate.Name, true) == 0 && ( string.Compare(r.FilePath, superPath, true) == 0 || // for within OneDrive r.FilePath.ToLower().Contains(delCandidate.FullName.ToLower()) // for without OneDrive ) ); if (dupeInDb != null) { if (dupeInDb.LastSeen != _ah.AuditTimeID.Date) { dupeInDb.LastSeen = _ah.AuditTimeID.Date; } } } var hash = await GetFileHashAsync(delCandidate, ha); lock (_thisLock) { _TotalHashed++; if (!db.FileDetails.Any(r => string.Compare(r.FileHash, hash) == 0)) { addNewFD(db, delCandidate, hash, $"<Exisiting file does not exist: {delCandidate.FullName}>\r\n"); } else { var dupeCount = db.FileDetails.Count(r => r.FileSize == delCandidate.Length); foreach (var exgByLen in db.FileDetails.Where(r => r.FileSize == delCandidate.Length).ToList()) // db.FileDetails.Where(r => string.Compare(r.FileHash, hash) == 0).ToList().ForEach(exgByHash => { var dbFullPathName = Path.Combine(oneDrBase, exgByLen.FilePath.StartsWith(@"\") ? exgByLen.FilePath.Substring(1) : exgByLen.FilePath); Trace.WriteLine($"{dupeCount} matches, \n {delCandidate.FullName} - fic\n {dbFullPathName} - db ver\n"); if (string.Compare(delCandidate.FullName, dbFullPathName, true) != 0) // only if not the same record in db { if (File.Exists(dbFullPathName)) // if found in db file exists at the db specified location - safe to delete the dupe { if (File.Exists(delCandidate.FullName)) // we could have deleted the dupe already. { if (string.Compare(exgByLen.FileHash, hash) == 0) { safeDeleteFile(dupeInDb, dbFullPathName, delCandidate.FullName, "<Deleted-Auto as Dupe by HASH. Org: {0}", delCandidate.Length); break; } else { var nu = new DelPopup(); _newUenWindowF.ShowNewWindow(nu, delCandidate.FullName, dbFullPathName, hash, exgByLen.FileHash, delCandidate.Length, exgByLen.FileSize); if (nu.OkToDelete == null) { OnRequestClose(); break; } else if (nu.OkToDelete.Value) { safeDeleteFile(dupeInDb, dbFullPathName, delCandidate.FullName, "<Deleted as Dupe by FileLen and Look. Org: {0}", delCandidate.Length); break; } } } } else // if the entry in DB does not exist in FS, update the DB with the current DUPE's values (for full local OneDrive mirror) { smartUpdateNote(exgByLen, $"<Moved from {exgByLen.FilePath}>\r\n"); exgByLen.LastSeen = _ah.AuditTimeID; exgByLen.FileName = delCandidate.Name; exgByLen.FileExtn = delCandidate.Extension; exgByLen.FilePath = superPath; _TotalRenamed++; } } } } } return(prgLen); }