/// <summary> /// Helper that unlocks the <see cref="AegisArchive"/> using the given <see cref="ArchiveUnlocker"/>. /// </summary> /// <param name="archive">The archive to unlock.</param> /// <param name="unlocker">The unlocking helper.</param> private static void UnlockArchive(AegisArchive archive, ArchiveUnlocker unlocker) { try { unlocker.Unlock(archive); } catch (ArchiveCorruptedException e) { throw new AegisUserErrorException( $"The archive file at {archive.FullFilePath} is corrupted: {e.Message}", innerException: e); } catch (UnauthorizedException e) { throw new AegisUserErrorException( $"The key was not able to unlock the archive.", innerException: e); } }
/// <summary> /// Helper that opens an <see cref="AegisArchive"/> from disk. /// </summary> /// <param name="archivePath">The path to the <see cref="AegisArchive"/> on disk.</param> /// <param name="tempDirectory">The temp directory that the archive can use for operations.</param> /// <param name="archiveUnlocker">The archive unlock helper. If null, archive will be opened but left unlocked.</param> /// <returns>The opened <see cref="AegisArchive"/>.</returns> private static AegisArchive OpenArchive(string archivePath, string tempDirectory, ArchiveUnlocker archiveUnlocker) { if (string.IsNullOrWhiteSpace(archivePath)) { throw new AegisUserErrorException( "Specify the path to the Aegis archive to open. Use the '-a' option or check the 'open' command help for details."); } AegisArchive archive = null; var openSuccess = false; try { var archiveFileSettings = new SecureArchiveFileSettings(archivePath, tempDirectory); archive = AegisArchive.Load(archiveFileSettings); if (archiveUnlocker != null) { UnlockArchive(archive, archiveUnlocker); } openSuccess = true; } catch (IOException e) { throw new AegisUserErrorException( $"Unable to read file at {archivePath}.", innerException: e); } catch (ArchiveCorruptedException e) { throw new AegisUserErrorException( $"The archive file at {archivePath} is corrupted: {e.Message}", innerException: e); } finally { if (!openSuccess) { // Make sure to release any holds if we couldn't successfully open the archive. archive?.Dispose(); } } return(archive); }