public void TestSetAuto() { using var flagDir = new TemporaryDirectory("0install-unit-tests"); FlagUtils.SetAuto(FlagUtils.XbitFile, Path.Combine(flagDir, "file1")); FlagUtils.SetAuto(FlagUtils.XbitFile, Path.Combine(flagDir, "dir", "file2")); File.ReadAllText(Path.Combine(flagDir, FlagUtils.XbitFile)).Should().Be("/file1\n/dir/file2\n"); }
public void TestRename() { using var flagFile = new TemporaryFile("0install-unit-tests"); File.WriteAllText(flagFile, "/dir/file1\n/dir/file2\n/dir2/file\n"); FlagUtils.Rename(flagFile, "dir", "new_dir"); File.ReadAllText(flagFile).Should().Be("/new_dir/file1\n/new_dir/file2\n/dir2/file\n"); }
public void TestSet() { using var flagFile = new TemporaryFile("0install-unit-tests"); FlagUtils.Set(flagFile, Path.Combine("dir1", "file1")); File.ReadAllText(flagFile).Should().Be("/dir1/file1\n"); FlagUtils.Set(flagFile, Path.Combine("dir2", "file2")); File.ReadAllText(flagFile).Should().Be("/dir1/file1\n/dir2/file2\n"); }
public void TestIsFlagged() { using var flagDir = new TemporaryDirectory("0install-unit-tests"); File.WriteAllText(Path.Combine(flagDir, FlagUtils.XbitFile), "/dir1/file1\n/dir2/file2\n"); FlagUtils.IsFlagged(FlagUtils.XbitFile, Path.Combine(flagDir, "dir1", "file1")).Should().BeTrue(); FlagUtils.IsFlagged(FlagUtils.XbitFile, Path.Combine(flagDir, "dir2", "file2")).Should().BeTrue(); FlagUtils.IsFlagged(FlagUtils.XbitFile, Path.Combine(flagDir, "dir1", "file2")).Should().BeFalse(); FlagUtils.IsFlagged(FlagUtils.SymlinkFile, Path.Combine(flagDir, "dir1", "file1")).Should().BeFalse(); }
/// <summary> /// Creates a new directory walking task. /// </summary> /// <param name="sourcePath">The path of the directory to walk.</param> protected DirectoryTaskBase(string sourcePath) { #region Sanity checks if (string.IsNullOrEmpty(sourcePath)) { throw new ArgumentNullException(nameof(sourcePath)); } #endregion SourceDirectory = new DirectoryInfo(Path.GetFullPath(sourcePath)); _sourceIsUnixFS = FlagUtils.IsUnixFS(sourcePath); }
/// <summary> /// Applies a <see cref="RenameStep"/> to a <see cref="TemporaryDirectory"/>. /// </summary> /// <param name="step">The <see cref="RenameStep"/> to apply.</param> /// <param name="workingDir">The <see cref="TemporaryDirectory"/> to apply the changes to.</param> /// <exception cref="IOException">A path specified in <paramref name="step"/> is illegal.</exception> public static void Apply(this RenameStep step, TemporaryDirectory workingDir) { #region Sanity checks if (step == null) { throw new ArgumentNullException(nameof(step)); } if (workingDir == null) { throw new ArgumentNullException(nameof(workingDir)); } #endregion if (string.IsNullOrEmpty(step.Source)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, "(empty)")); } if (string.IsNullOrEmpty(step.Destination)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, "(empty)")); } string source = FileUtils.UnifySlashes(step.Source); string destination = FileUtils.UnifySlashes(step.Destination); if (FileUtils.IsBreakoutPath(source)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, source)); } if (FileUtils.IsBreakoutPath(destination)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, destination)); } string sourcePath = Path.Combine(workingDir, source); string destinationPath = Path.Combine(workingDir, destination); string parentDir = Path.GetDirectoryName(destinationPath); if (!string.IsNullOrEmpty(parentDir) && !Directory.Exists(parentDir)) { Directory.CreateDirectory(parentDir); } if (Directory.Exists(sourcePath)) { Directory.Move(sourcePath, destinationPath); } else { File.Move(sourcePath, destinationPath); } // Update in flag files as well FlagUtils.Rename(Path.Combine(workingDir, FlagUtils.XbitFile), source, destination); FlagUtils.Rename(Path.Combine(workingDir, FlagUtils.SymlinkFile), source, destination); }
public void TestRemove() { using var flagFile = new TemporaryFile("0install-unit-tests"); File.WriteAllText(flagFile, "/dir1/file1\n/dir2/file2\n"); FlagUtils.Remove(flagFile, "dir"); File.ReadAllText(flagFile).Should().Be("/dir1/file1\n/dir2/file2\n", because: "Partial match should not change anything"); FlagUtils.Remove(flagFile, Path.Combine("dir1", "file1")); File.ReadAllText(flagFile).Should().Be("/dir2/file2\n"); FlagUtils.Remove(flagFile, "dir2"); File.ReadAllText(flagFile).Should().Be(""); }
public void TestIsUnixFS() { using var tempDir = new TemporaryDirectory("0install-unit-tests"); if (UnixUtils.IsUnix) { FlagUtils.IsUnixFS(tempDir).Should().BeTrue(); FlagUtils.MarkAsNoUnixFS(tempDir); FlagUtils.IsUnixFS(tempDir).Should().BeFalse(); } else { FlagUtils.IsUnixFS(tempDir).Should().BeFalse(); } }
public void TestGetFiles() { using var flagDir = new TemporaryDirectory("0install-unit-tests"); File.WriteAllText(Path.Combine(flagDir, FlagUtils.XbitFile), "/dir1/file1\n/dir2/file2\n"); var expectedResult = new[] { Path.Combine(flagDir, "dir1", "file1"), Path.Combine(flagDir, "dir2", "file2") }; FlagUtils.GetFiles(FlagUtils.XbitFile, flagDir) .Should().BeEquivalentTo(expectedResult, because: "Should find .xbit file in same directory"); FlagUtils.GetFiles(FlagUtils.XbitFile, Path.Combine(flagDir, "subdir")) .Should().BeEquivalentTo(expectedResult, because: "Should find .xbit file in parent directory"); }
/// <summary> /// Iterates over all <paramref name="entries"/> and calls handler methods for them. /// </summary> /// <exception cref="NotSupportedException">A file has illegal properties (e.g. is a device file, has line breaks in the filename, etc.).</exception> /// <exception cref="IOException">There was an error reading a file.</exception> /// <exception cref="UnauthorizedAccessException">You have insufficient rights to read a file.</exception> protected virtual void HandleEntries(IEnumerable <FileSystemInfo> entries) { var externalXbits = FlagUtils.GetFiles(FlagUtils.XbitFile, SourceDirectory.FullName); var externalSymlinks = FlagUtils.GetFiles(FlagUtils.SymlinkFile, SourceDirectory.FullName); foreach (var entry in entries ?? throw new ArgumentNullException(nameof(entries))) { CancellationToken.ThrowIfCancellationRequested(); switch (entry) { case FileInfo file when file.Name != Manifest.ManifestFile && file.Name != FlagUtils.XbitFile && file.Name != FlagUtils.SymlinkFile: HandleEntry(file, externalXbits, externalSymlinks); UnitsProcessed += file.Length; break; case DirectoryInfo directory: HandleEntry(directory); break; } } }
/// <summary> /// Applies a <see cref="RemoveStep"/> to a <see cref="TemporaryDirectory"/>. /// </summary> /// <param name="step">The <see cref="RemoveStep"/> to apply.</param> /// <param name="workingDir">The <see cref="TemporaryDirectory"/> to apply the changes to.</param> /// <exception cref="IOException">A path specified in <paramref name="step"/> is illegal.</exception> public static void Apply(this RemoveStep step, TemporaryDirectory workingDir) { #region Sanity checks if (step == null) { throw new ArgumentNullException(nameof(step)); } if (workingDir == null) { throw new ArgumentNullException(nameof(workingDir)); } #endregion if (string.IsNullOrEmpty(step.Path)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, "(empty)")); } string path = FileUtils.UnifySlashes(step.Path); if (FileUtils.IsBreakoutPath(path)) { throw new IOException(string.Format(Resources.RecipeInvalidPath, path)); } string absolutePath = Path.Combine(workingDir, path); if (Directory.Exists(absolutePath)) { Directory.Delete(absolutePath, recursive: true); } else { File.Delete(absolutePath); } // Update in flag files as well FlagUtils.Remove(Path.Combine(workingDir, FlagUtils.XbitFile), path); FlagUtils.Remove(Path.Combine(workingDir, FlagUtils.SymlinkFile), path); }