public static void Rename([NotNull] string path, [NotNull] string source, [NotNull] string destination)
{
#region Sanity checks
if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path));
if (string.IsNullOrEmpty(source)) throw new ArgumentNullException(nameof(source));
if (Path.IsPathRooted(source)) throw new ArgumentException(Resources.PathNotRelative, nameof(source));
if (string.IsNullOrEmpty(destination)) throw new ArgumentNullException(nameof(destination));
if (Path.IsPathRooted(destination)) throw new ArgumentException(Resources.PathNotRelative, nameof(destination));
#endregion
if (!File.Exists(path)) return;
// Convert paths to rooted Unix-style
source = "/" + source.Replace(Path.DirectorySeparatorChar, '/');
destination = "/" + destination.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 == source || line.StartsWith(source + "/"))
newFlagFile.WriteLine(destination + line.Substring(source.Length));
else newFlagFile.WriteLine(line);
}
}
atomic.Commit();
}
}
#endregion