/// <summary> /// Search a directory or archive and add any files or sub-directories /// to the list of candidates currently being resolved /// </summary> /// <param name="candidates">Candidates currently being resolved</param> public void Explode(SubmissionCandidates candidates, string workingDirectory) { if (IsArchive) { // Archives in Archives!! if (IsArchivedFile) { string exportPath = Path.Combine(workingDirectory, HunterConfig.GalileoDefaultDataFolder + Path.DirectorySeparatorChar + "galileo_" + Guid.ToString() + FileExtension); ArchiveInfo.Extract(ReadPath, exportPath); _resolvePath = exportPath; } if (_fileType == FileTypes.Types.Zip) { _archiveInfo = new ZipArchiveInfo(ReadPath); _archiveChildren = new List <SubmissionCandidate>(10); if (_archiveInfo.IsOpen == false) { // Probably best to do something better than this? throw new Exception("Could not open archive???"); } foreach (var fileName in _archiveInfo) { SubmissionCandidate candidate = candidates.Add(fileName, _depth + 1, this); _archiveChildren.Add(candidate); } // We leave _archiveInfo open, because later the files // will want to be inspected and possibly extracted. } } else if (IsDirectory) { System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(ReadPath); foreach (var file in info.EnumerateFiles()) { candidates.Add(file.FullName, _depth + 1, this); } foreach (var directory in info.EnumerateDirectories()) { candidates.Add(directory.FullName, _depth + 1, this); } } }
/// <summary> /// Resolve this candidate and work out what it is; A File, Directory or Archive. /// Once that it is checked against the filters to see if it can be processed /// any futher. /// /// If it's a file then the type is worked out. /// If it's a directory or archive, then it may be 'exploded' so sub-directories /// and files will be added as candidates. /// /// After all of that _resolution contains the resolved state of the Candidate. /// </summary> /// <param name="candidates">Candidates currently being resolved</param> /// <returns></returns> public SubmissionCandidateResolution Resolve(SubmissionCandidates candidates, string workingDirectory) { #if DEBUG Console.WriteLine(ReadPath); #endif // Start off with nothing _resolution = SubmissionCandidateResolution.None; bool isValidFile = false, isArchive = false, isFileSystem = false, isInArchive = false, isIgnoredButExported = false; bool shouldExplode = false; // @TODO Detect parents, etc. if (Parent != null) { if (Parent.IsArchive) { isInArchive = true; } } if (isInArchive == false) { // Is this a directory? if (FileTypes.Types.IsFileSystem(ReadPath)) { // Yes, it's a directory. isFileSystem = true; _containerPath = ReadPath; } else { // What about an Archive? if (FileTypes.Types.IsArchive(ReadPath, out _fileType)) { isArchive = true; } // Then it must be a file. // But before we start processing, we need to check to // see if it's a supported file. If not then it's ignored. else if (FileTypes.Types.TryGet(ReadPath, out _fileType)) { if (_fileType != FileTypes.Types.OctetStream) { isValidFile = true; } } } } else { // What if it's an archive in an archive?? - That's just crazy talk!! if (FileTypes.Types.IsArchiveInArchive(ReadPath, ArchiveInfo, out _fileType)) { isArchive = true; } else { if (FileTypes.Types.TryGetInArchive(ReadPath, ArchiveInfo, out _fileType)) { if (_fileType == FileTypes.Types.OctetStream) { isIgnoredButExported = true; } else { isValidFile = true; } } else { isIgnoredButExported = true; } } } // Is it an archive? // Mark it as an archive, and put up a flag so it can be exploded later. if (isArchive) { _resolution |= SubmissionCandidateResolution.Archive; shouldExplode = true; } // Is it a directory // Mark it as a directory, and put up a flag so it can be exploded later else if (isFileSystem) { _resolution |= SubmissionCandidateResolution.Directory; shouldExplode = true; } // Is it a valid file? // Then it's an submission else if (isValidFile) { _resolution |= SubmissionCandidateResolution.Submission; } if (isIgnoredButExported) { _resolution |= SubmissionCandidateResolution.IgnoreButExport; } // Apply the filters now bool filtered = false; // Hotfix for now, since .zip files are being applied as no-nos by the config if (isArchive == false) { filtered = candidates.ApplyFilter(this); } if (filtered) { _resolution |= SubmissionCandidateResolution.Filtered; } // If we need to explode and we aren't filtered, then explode! if (shouldExplode && filtered == false) { Explode(candidates, workingDirectory); } // Finally mark as resolved _resolution |= SubmissionCandidateResolution.Resolved; return(_resolution); }