/// <summary> /// Run the FileCorrupter once /// </summary> /// <param name="fc">FileCorrupter object</param> /// <param name="corruptType">Enum stating which corrupt type to use</param> /// <param name="n">n value for fc</param> /// <param name="oldByte">oldByte for fc</param> /// <param name="newByte">newByte for fc</param> private void corruptOne(FileCorruptor fc, CorruptionType corruptType, int n, byte oldByte, byte newByte) { //run the corrupt if (corruptType == CorruptionType.Insert) { fc.InsertCorrupt(oldByte, newByte, n); } else if (corruptType == CorruptionType.Delete) { fc.DeleteCorrupt(oldByte, n); } else { fc.ReplaceCorrupt(oldByte, newByte, n); } }
/// <summary> /// Runs the corruptor on a background thread /// </summary /// <param name="callback">Void-returning method to call on progress updates. 1st param is double, 2nd param is FileInfo</param> public void Run(Action <double, FileInfo> callback = null) { //initialize the background worker Console.Log("CorruptManager: Initializing background worker"); worker = new Thread(() => { try { FileCorruptor fc = new FileCorruptor(null, null, startByte, endByte); //corrupt all the files, or corrupt only certain types? if (fileTypes == null) { Console.Log("CorruptManager: Using predefined queue, beginning corruption"); //corrupt each file in the list for (int i = 0; i < queue.Length; i++) { FileInfo f = queue[i]; //if the corruptor is corrupting a batch of singletons, newname is different string newName; if (rootfolder == null) { newName = outFolder + Path.DirectorySeparatorChar + f.Name; } else { newName = f.FullName.Replace(rootfolder, outFolder); } fc.updateFiles(f, new FileInfo(newName)); //create necessary folders Directory.CreateDirectory(newName.Replace(f.Name, "")); //corrupt the file corruptOne(fc, corruptType, n, oldByte, newByte); //progress update if (i == queue.Length - 1 || i % 10 == 0) { callback?.Invoke((double)(i + 1) / queue.Length * 100, f); } } } else { Console.Log("CorruptManager: Using queue hashset rules, beginning corruption"); for (int i = 0; i < queue.Length; i++) { FileInfo f = queue[i]; //corrupt if file type is compatible //if not inverting and has type, or inverting and does not have type if ((fileTypes.Contains(f.Extension) && !invertFiletypes) || (invertFiletypes && !fileTypes.Contains(f.Extension))) { string newName = f.FullName.Replace(rootfolder, outFolder); fc.updateFiles(f, new FileInfo(newName)); //create necessary directories Directory.CreateDirectory(newName.Replace(f.Name, "")); //corrupt the file corruptOne(fc, corruptType, n, oldByte, newByte); //progress update if (i == queue.Length - 1 || i % 10 == 0) { callback?.Invoke((double)(i) / queue.Length * 100, f); } } //otherwise copy the file else { string newName = f.FullName.Replace(rootfolder, outFolder); //create necessary directories Directory.CreateDirectory(newName.Replace(f.Name, "")); //copy the file, overwrite it if necessary try { File.Copy(f.FullName, newName, true); } //if unable to copy, catch silently catch (Exception e) { Console.LogException(e); } } } } //invoke 100% progress when complete callback?.Invoke(100, null); } catch (Exception e) { Console.LogException(e, "See the run method."); } }); worker.Start(); }