public override ExitCode Execute() { try { switch (AdditionalArgs.Count) { case 1: // Verify a directory inside the store ImplementationStore.Verify(new ManifestDigest(AdditionalArgs[0]), Handler); break; case 2: // Verify an arbitrary directory ImplementationStoreUtils.Verify(AdditionalArgs[0], new ManifestDigest(AdditionalArgs[1]), Handler); break; } } catch (DigestMismatchException ex) { Handler.Output(Resources.VerifyImplementation, ex.LongMessage); return(ExitCode.DigestMismatch); } return(ExitCode.OK); }
/// <inheritdoc/> public void Add(ManifestDigest manifestDigest, Action <IBuilder> build) { #region Sanity checks if (build == null) { throw new ArgumentNullException(nameof(build)); } #endregion if (manifestDigest.AvailableDigests.Any(digest => Directory.Exists(System.IO.Path.Combine(Path, digest)))) { throw new ImplementationAlreadyInStoreException(manifestDigest); } string expectedDigest = manifestDigest.Best ?? throw new NotSupportedException(Resources.NoKnownDigestMethod); Log.Debug($"Storing implementation {expectedDigest} in {this}"); var format = ManifestFormat.FromPrefix(manifestDigest.Best); // Place files in temp directory until digest is verified string tempDir = GetTempDir(); using var _ = new Disposable(() => DeleteTempDir(tempDir)); var builder = new ManifestBuilder(format); build(new DirectoryBuilder(tempDir, builder)); var manifest = ImplementationStoreUtils.Verify(builder.Manifest, expectedDigest); if (manifest == null) { throw new DigestMismatchException( expectedDigest, actualDigest: builder.Manifest.CalculateDigest(), actualManifest: builder.Manifest); } manifest.Save(System.IO.Path.Combine(tempDir, Manifest.ManifestFile)); string target = System.IO.Path.Combine(Path, expectedDigest); lock (_renameLock) // Prevent race-conditions when adding the same digest twice { if (Directory.Exists(target)) { throw new ImplementationAlreadyInStoreException(manifestDigest); } // Move directory to final destination try { Directory.Move(tempDir, target); } catch (IOException ex) when(ex.Message.Contains("already exists") || Directory.Exists(target)) { throw new ImplementationAlreadyInStoreException(manifestDigest); } } // Prevent any further changes to the directory if (UseWriteProtection) { EnableWriteProtection(target); } }
/// <summary> /// Determines whether a directory resides on a non-Unix filesystem. /// </summary> /// <param name="directoryPath">The full path to the directory.</param> /// <remarks>The flag file is searched for instead of specifying it directly to allow handling of special cases like creating manifests of subdirectories of extracted archives.</remarks> /// <seealso cref="NoUnixFSFile"/> /// <seealso cref="FileUtils.IsUnixFS"/> public static bool IsUnixFS(string directoryPath) { #region Sanity checks if (string.IsNullOrEmpty(directoryPath)) { throw new ArgumentNullException(nameof(directoryPath)); } #endregion // Move up one level to avoid write-protection within implementation directories string?implementationPath = ImplementationStoreUtils.DetectImplementationPath(directoryPath); if (implementationPath != null) { directoryPath = Path.Combine(implementationPath, ".."); } try { if (FindRootDir(NoUnixFSFile, directoryPath) != null) { return(false); } else { return(FileUtils.IsUnixFS(directoryPath)); } } #region Error handling catch (IOException) { // Just assume the target is a Unix FS if the check fails on a Unixoid OS return(UnixUtils.IsUnix); } catch (UnauthorizedAccessException) { // Just assume the target is a Unix FS if the check fails on a Unixoid OS return(UnixUtils.IsUnix); } #endregion }
public void ImplementationSubDirPath() { ImplementationStoreUtils.IsImplementation(WindowsUtils.IsWindows ? @"C:\some\dir\sha1new=123\subdir" : "/some/dir/sha1new=123/subdir", out string?path) .Should().BeTrue(); path.Should().Be(WindowsUtils.IsWindows ? @"C:\some\dir\sha1new=123" : "/some/dir/sha1new=123"); }
public void NotImplementationPath() { ImplementationStoreUtils.IsImplementation(WindowsUtils.IsWindows ? @"C:\some\dir" : "/some/dir", out _) .Should().BeFalse(); }