private void AddFiles(string driveUnitLetter, IsoFolder currentFolder, FileScanResult fileScanResult, IsoVolume iso) { string[] filePaths; try { filePaths = DirectoryProvider.GetFiles(currentFolder.Path.Replace("/", driveUnitLetter + @":\"), "*.*"); fileScanResult.ProcessedFolderCount += 1; } catch (UnauthorizedAccessException) { fileScanResult.Log.AppendLine(string.Format("ERROR folder-access rights-:'{0}'", currentFolder.Path)); fileScanResult.FoldersWithErrorCount += 1; return; } catch (Exception) { fileScanResult.Log.AppendLine(string.Format("ERROR folder-unknown-:'{0}'", currentFolder.Path)); fileScanResult.FoldersWithErrorCount += 1; return; } foreach (var filePath in filePaths) { IsoFile isoFile; try { isoFile = FileProvider.GetIsoFile(filePath); isoFile.IsoVolumeId = iso.Id; } catch (UnauthorizedAccessException) { fileScanResult.Log.AppendLine(string.Format("ERROR file-access rights-:'{0}'", filePath)); fileScanResult.FilesWithErrorCount += 1; continue; } catch (Exception) { fileScanResult.Log.AppendLine(string.Format("ERROR file-unknown-:'{0}'", filePath)); fileScanResult.FilesWithErrorCount += 1; continue; } isoFile.Parent = currentFolder; fileScanResult.ProcessedFileCount += 1; currentFolder.ChildFiles.Add(isoFile); fileScanResult.Log.AppendLine( string.Format( "FILE name:'{0}', extension:'{1}', path:'{2}', dateCreated:{3}, dateModified:{4}, size:{5}", isoFile.Name, isoFile.Extension, filePath, isoFile.Created, isoFile.Modified, GetSizeString(isoFile.Size) )); } }
public void ConstructorShouldInitializeCollectionProperties() { // Arrange IsoFolder isoFolder; // Act isoFolder = new IsoFolder(); // Assert Assert.IsNotNull(isoFolder.ChildFiles); Assert.IsNotNull(isoFolder.ChildFolders); }
private void AddFolders(string driveUnitLetter, IsoFolder currentFolder, List <IsoFolder> folders, IsoVolume iso) { var folderPaths = DirectoryProvider.GetDirectories(currentFolder.Path.Replace("/", driveUnitLetter + @":\")); foreach (var path in folderPaths) { var childFolder = new IsoFolder { Name = Path.GetFileName(path), Path = path, Parent = currentFolder, IsoVolumeId = iso.Id }; childFolder.Path = childFolder.Path.Replace(driveUnitLetter + @":\", "/"); currentFolder.ChildFolders.Add(childFolder); folders.Add(childFolder); } }
public IsoFolder ScanFolderForFiles(string driveUnitLetter, FileScanResult fileScanResult, IsoVolume iso) { string fullDrivePath = driveUnitLetter + ":\\"; var currentTime = DateTime.Now; var rootFolder = new IsoFolder { Name = "/", Path = fullDrivePath.Replace(driveUnitLetter + @":\", "/"), Parent = null, IsoVolumeId = iso.Id }; var folders = new List <IsoFolder> { rootFolder }; for (int i = 0; i < folders.Count; i++) { var currentFolder = folders[i]; AddFiles(driveUnitLetter, currentFolder, fileScanResult, iso); AddFolders(driveUnitLetter, currentFolder, folders, iso); } fileScanResult.TotalTime = DateTime.Now.Subtract(currentTime); fileScanResult.Log.AppendLine( string.Format( "Scan finished:{0}hs:{1}min:{2}sec:{3}msec, files:{4}, fileErrors:{5}, folders:{6}, folderErrors:{7}", fileScanResult.TotalTime.Hours, fileScanResult.TotalTime.Minutes, fileScanResult.TotalTime.Seconds, fileScanResult.TotalTime.Milliseconds, fileScanResult.ProcessedFileCount, fileScanResult.FilesWithErrorCount, fileScanResult.ProcessedFolderCount, fileScanResult.FoldersWithErrorCount)); return(rootFolder); }
public async void Execute() { txtConsole.Text = string.Empty; lblInformation.Visible = false; ShowLogMessage("Scan started..."); TotalScanStartTime = DateTime.Now; var currentIsoFilePathIndex = 0; var isoFilesCount = _isoScannerInfo.SelectedIsoFileNames.Count(); var isoFilesFoundMessage = string.Format("{0} iso files found", isoFilesCount); ShowInformationMessage(isoFilesFoundMessage); ShowLogMessage(isoFilesFoundMessage); var unmountResult = await UnMount(); if (!unmountResult) { ShowLogMessage(string.Format("Error trying to unmount file from unit. Process aborted.")); return; } foreach (var isoFileName in _isoScannerInfo.SelectedIsoFileNames) { var isoFilePath = Path.Combine(_isoScannerInfo.IsoFolderPath, isoFileName); var mountResult = await VirtualCloneDrive.MountAsync(isoFilePath); if (mountResult.HasError) { ShowLogMessage(string.Format("Error trying to mount: {0}", Path.GetFileName(isoFilePath))); } else { var volumeName = VirtualCloneDrive.VolumeLabel; ShowLogMessage(string.Format("Scanning: {0}...", volumeName)); ShowInformationMessage(string.Format("Scanning: {0} [{1} of {2}]", volumeName, currentIsoFilePathIndex + 1, isoFilesCount)); if (!_unitOfWork.IsoVolumeRepository.Search(i => i.VolumeLabel == volumeName).Any()) { var initialFolder = new IsoFolder { Name = "$", Path = "/", IsoVolumeId = 0 }; var iso = new IsoVolume { Created = DateTime.Now, FileName = Path.GetFileName(isoFileName), Size = Convert.ToDecimal(VirtualCloneDrive.TotalSize), VolumeLabel = volumeName, FileCount = 0, RootFolder = initialFolder }; _unitOfWork.IsoVolumeRepository.Insert(iso); _unitOfWork.Save(); var fileScanResult = new FileScanResult(); try { var rootFolder = _fileScanner.ScanFolderForFiles(VirtualCloneDrive.UnitLetter, fileScanResult, iso); iso.FileCount = fileScanResult.ProcessedFileCount; iso.RootFolder = rootFolder; } catch (Exception ex) { ShowLogMessage(string.Format("Error scanning iso file: {0}", Path.GetFileName(isoFilePath))); ShowLogMessage(ex.Message); } ShowLogMessage(string.Format( "Scan finished for {0} ({1}), scan duration: {2}, files processed: {3}", volumeName, iso.FileName, fileScanResult.TotalTime.ToString("hh\\:mm\\:ss"), fileScanResult.ProcessedFileCount)); try { ShowLogMessage("Saving changes to the database..."); _unitOfWork.IsoVolumeRepository.Update(iso); _unitOfWork.IsoFolderRepository.Delete(initialFolder); _unitOfWork.Save(); ShowLogMessage("Changes successfully saved"); } catch (Exception ex) { _unitOfWork.IsoVolumeRepository.Delete(iso); _unitOfWork.IsoFolderRepository.Delete(initialFolder); _unitOfWork.Save(); ShowLogMessage("An error has ocurred saving changes to database"); fileScanResult.Log.Append(ex.Message); fileScanResult.Log.Append(ex.StackTrace); } try { ShowLogMessage("Saving log file..."); SaveScanLog(iso.FileName, fileScanResult.Log.ToString()); ShowLogMessage(string.Format("Log file successfully saved for {0} ", iso.FileName)); } catch { ShowLogMessage("Failed to save log file"); ShowLogMessage(fileScanResult.Log.ToString()); } } else { ShowLogMessage(string.Format("Iso Volume '{0}' already exists in the database.", volumeName)); } unmountResult = await UnMount(); if (!unmountResult) { ShowLogMessage(string.Format("Error trying to unmount '{0}'. Process aborted.", isoFilePath)); break; } } currentIsoFilePathIndex += 1; } ShowLogMessage("Scan finished"); var totalTime = string.Format("Total time: {0}", DateTime.Now.Subtract(TotalScanStartTime).ToString("hh\\:mm\\:ss")); ShowLogMessage(totalTime); ShowInformationMessage(string.Format("Scan finished. {0}", totalTime)); if (WorkFinished != null) { WorkFinished(); } }