public static Remove ( [ path, [ relativePath ) : void | ||
path | [ | The full path to the flag file, named |
relativePath | [ | The path of the file or directory to remove relative to |
Результат | void |
public static void Remove([NotNull] string path, [NotNull] string relativePath)
{
#region Sanity checks
if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path));
if (string.IsNullOrEmpty(relativePath)) throw new ArgumentNullException(nameof(relativePath));
if (Path.IsPathRooted(relativePath)) throw new ArgumentException(Resources.PathNotRelative, nameof(relativePath));
#endregion
if (!File.Exists(path)) return;
// Convert path to rooted Unix-style
string unixPath = "/" + relativePath.Replace(Path.DirectorySeparatorChar, '/');
using (var atomic = new AtomicWrite(path))
using (var newFlagFile = new StreamWriter(atomic.WritePath, append: false, encoding: FeedUtils.Encoding) {NewLine = "\n"})
using (var oldFlagFile = File.OpenText(path))
{
// Each line in the file signals a flagged file
while (!oldFlagFile.EndOfStream)
{
string line = oldFlagFile.ReadLine();
if (line != null && line.StartsWith("/"))
{
if (line == unixPath || line.StartsWith(unixPath + "/")) continue; // Filter out removed files
newFlagFile.WriteLine(line);
}
}
atomic.Commit();
}
}
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(""); } }