private static void VirtualizePath(PathInfo pathInfo, string rootPath) { // TODO Prevent infinite loop when the first folder after root is too long // If we already have symLinkPaths it means we are on the second (or more) iteration and need virtualize PhysicalPath because it still is too long. // string virtualizedPath = pathInfo.AllSymlinkPaths.Count > 0 ? pathInfo.PhysicalPath : pathInfo.VirtualPath; string reparsePointPath = PathAnalyzer.GetAllowedLengthPath(pathInfo.PhysicalPath); DirectoryInfo reparsePodintDi = new DirectoryInfo(reparsePointPath); // This will be our new physical path string dest = CreateDistDir(rootPath); if (!reparsePodintDi.Exists) { // Create parent directory which will contain symlink string parentDir = Path.GetDirectoryName(reparsePodintDi.FullName); Directory.CreateDirectory(parentDir); } else { // TODO What to do if symlink is already there? How can it happen? // if (SymLinkUtility.IsSymLinkExists(allowedLengthPath) { return allowedLengthPath; } bool moved = MoveDirContent(reparsePointPath, dest); if (moved) { reparsePodintDi.Delete(); } } Symlink.Create(reparsePointPath, dest); pathInfo.PhysicalPath = GetFullPhysicalPath(pathInfo.VirtualPath, reparsePointPath, dest); }
static void Main(string[] args) { // Test MAX_LENGTH = 30 // rootPath Length = 7 string rootPath = @"E:\temp"; string tooLongTwicePath = Path.Combine(rootPath, @"111111111\222222222\333333333\444444444\555555555\666666666"); if (PathAnalyzer.CanMap(tooLongTwicePath, rootPath, PathVirtualizer.GetDistDirName())) { PathInfo pathInfo2 = PathVirtualizer.Virtualize(tooLongTwicePath, rootPath); Log(pathInfo2); } Console.ReadKey(); }