public void Save(IEnumerable <object> items, IEnumerable <IDetector> detectors, IDataReaderPool dataReaderPool, string directory, IProgressReporter progressReporter, bool createForensicIntegrityLog) { PreConditions.Argument("items").Value(items).IsNotNull().And.IsNotEmpty(); PreConditions.Argument("detectors").Value(detectors).IsNotNull().And.DoesNotContainNull(); PreConditions.Argument("dataReaderPool").Value(dataReaderPool).IsNotNull(); PreConditions.Argument("directory").Value(directory).IsNotNull().And.IsNotEmpty(); PreConditions.Argument("progressReporter").Value(progressReporter).IsNotNull(); if (progressReporter.CancellationPending) { return; } var overallProgressReporter = new OverallProgressReporter(progressReporter); overallProgressReporter.CountNumberOfParts(items); var handledContainers = new HandledContainers(); int numSavedFiles = 0; handledContainers.ClearHandledFragmentedContainers(); foreach (object item in items) { string path = Path.Combine(directory, ReplaceIllegalPathCharactersByUnderscore(GetFileName(item))); numSavedFiles += SaveItem(item, dataReaderPool, path, overallProgressReporter, handledContainers, createForensicIntegrityLog); if (overallProgressReporter.CancellationPending) { break; } } }
/// <summary> /// Writes <paramref name="detectable"/> to file with name <paramref name="fileName"/>. /// </summary> /// <remarks> /// WARNING call SaveExtensions.ClearHandledFragmentedContainers(); before you call this method /// to clear the handled fragment containers /// </remarks> /// <param name="detectable">the detectable to write</param> /// <param name="fileName">the name of the file to write to/param> /// <returns>true when the file is written, else false</returns> private bool Write(IFragment detectable, IDataReaderPool dataReaderPool, string directory, IProgressReporter progressReporter, HandledContainers handledContainers, bool createForensicIntegrityLog) { if (detectable == null) { throw new ArgumentNullException("detectable"); } if (string.IsNullOrEmpty(directory)) { throw new ArgumentNullException("directory"); } string path = directory; if (handledContainers.FragmentedStreamAlreadySaved(detectable as IFragment)) { return(false); } if (progressReporter != null) { progressReporter.ReportProgress(0, "Gathering data"); } IDataPacket data = GetCompleteFragmentData(detectable, progressReporter, dataReaderPool); if (progressReporter.CancellationPending) { return(false); } long totalBytes = data.Length; path += string.Format("_{0}", data.StartOffset.ToString("X16")); if (detectable is IDataBlock) { long length = ((detectable as IFragment).FragmentContainer == null) ? detectable.Length : (detectable as IFragment).FragmentContainer.Length; path += string.Format("-{0}", (data.StartOffset + length).ToString("X16")); } path += (detectable.Detectors != null && detectable.Detectors.Count() > 0) ? detectable.Detectors.First().OutputFileExtension : ".bin"; bool detectableWritten = false; try { long handledBytes = 0L; if (progressReporter != null) { string message = string.Format("Saving file: {0}", Path.GetFileName(path)); progressReporter.ReportProgress((totalBytes == 0) ? 0 : (int)((handledBytes * 100) / totalBytes), message); } using (ResultWriter writer = new ResultWriter(File.Create(path), dataReaderPool)) { writer.WriteDataPacket(data, progressReporter, ref handledBytes, totalBytes); detectableWritten = true; } } catch (Exception e) { string message = string.Format("Failed to write {0} to {1}", ((detectable is ICodecStream) ? ReplaceIllegalPathCharactersByUnderscore((detectable as ICodecStream).Name) : string.Empty), path); throw new FrameworkException(message, e); } try { if (createForensicIntegrityLog) { string logFileName = string.Format("{0}.csv", path); using (FileStream fs = new FileStream(logFileName, FileMode.Create, FileAccess.Write, FileShare.Read)) { _forensicIntegrityLog.Log(data, detectable.Detectors, path, fs, ForensicLogType.CopiedData); } } } catch (Exception e) { string message = string.Format("Failed to write log file {0}.cvs ({1})", path, e.Message); throw new FrameworkException(message, e); } return(detectableWritten); }
/// <summary> /// Saves the <paramref name="codecStream"/> in the given <paramref name="path"/>. /// </summary> /// <param name="path">The directory to save the file to</param> /// <param name="codecStream">The <see cref="ICodecStream"/> to save</param> /// <returns>The number of files saved</returns> private int SaveCodecStream(ICodecStream codecStream, IDataReaderPool dataReaderPool, string path, OverallProgressReporter overallProgressReporter, HandledContainers handledContainers, bool createForensicIntegrityLog) { return(Write(codecStream, dataReaderPool, path, overallProgressReporter, handledContainers, createForensicIntegrityLog) == true ? 1 : 0); }
/// <summary> /// Saves a <paramref name="dataBlock"/> and its codec streams. /// </summary> /// <param name="dataBlock">The data block to save</param> /// <param name="dataReaderPool">The shared pool of file data readers</param> /// <param name="path">The directory in which to store the files</param> /// <param name="overallProgressReporter"></param> /// <param name="handledContainers"></param> /// <param name="forensicIntegrityLogFile"></param> /// <returns>The number of files saved</returns> private int SaveDataBlock(IDataBlock dataBlock, IDataReaderPool dataReaderPool, string path, OverallProgressReporter overallProgressReporter, HandledContainers handledContainers, bool createForensicIntegrityLog) { if (string.IsNullOrEmpty(path) || dataBlock == null) { return(0); } int numFullFiles = 0; path += string.Format("_{0}", ReplaceIllegalPathCharactersByUnderscore(dataBlock.Detectors.First().Name)); if (Write(dataBlock, dataReaderPool, path, overallProgressReporter, handledContainers, createForensicIntegrityLog)) { numFullFiles++; // Save all detectables foreach (ICodecStream codecStream in dataBlock.CodecStreams) { string fileName = string.Format("{0}_extracted_{1}_stream", path, ReplaceIllegalPathCharactersByUnderscore(codecStream.DataFormat.ToString())); numFullFiles += SaveCodecStream(codecStream, dataReaderPool, fileName, overallProgressReporter, handledContainers, createForensicIntegrityLog); } } return(numFullFiles); }
/// <summary> /// Saves all data blocks (and codec streams) for one <paramref name="inputFile"/>. /// </summary> /// <param name="inputFile">The input file to save</param> /// <param name="path">The directory in which to store the files</param> /// <returns>The number of files saved</returns> private int SaveInputFile(IInputFile inputFile, IDataReaderPool dataReaderPool, string path, OverallProgressReporter overallProgressReporter, HandledContainers handledContainers, bool createForensicIntegrityLog) { int numFullFiles = 0; foreach (IDataBlock dataBlock in inputFile.Project.GetDataBlocks(inputFile)) { numFullFiles += SaveDataBlock(dataBlock, dataReaderPool, path, overallProgressReporter, handledContainers, createForensicIntegrityLog); } return(numFullFiles); }
private int SaveItem(object item, IDataReaderPool dataReaderPool, string path, OverallProgressReporter overallProgressReporter, HandledContainers handledContainers, bool createForensicIntegrityLog) { IInputFile inputFile = item as IInputFile; if (inputFile != null) { return(SaveInputFile(inputFile, dataReaderPool, path, overallProgressReporter, handledContainers, createForensicIntegrityLog)); } IDataBlock dataBlock = item as IDataBlock; if (dataBlock != null) { return(SaveDataBlock(dataBlock, dataReaderPool, path, overallProgressReporter, handledContainers, createForensicIntegrityLog)); } ICodecStream codecStream = item as ICodecStream; if (codecStream != null) { path = string.Format("{0}__{1}", path, ReplaceIllegalPathCharactersByUnderscore(codecStream.Name)); return(SaveCodecStream(codecStream, dataReaderPool, path, overallProgressReporter, handledContainers, createForensicIntegrityLog)); } return(0); // TODO: is this an error? }