/// <summary> /// Creates a new symbolic link to a file or directory. /// </summary> /// <param name="sourcePath">The path of the link to create.</param> /// <param name="targetPath">The path of the existing file or directory to point to (relative to <paramref name="sourcePath" />).</param> public static void CreateSymlink(string sourcePath, string targetPath) { try { FileUtils.CreateSymlink(sourcePath, targetPath); } catch (IOException) when(WindowsUtils.IsWindows) { Log.Debug("Creating Cygwin symlink instead of NTFS symlink due to insufficient permissions: " + sourcePath); CygwinUtils.CreateSymlink(sourcePath, targetPath); } }
public override void Build(string parentPath) { string path = Path.Combine(parentPath, Name); if (UnixUtils.IsUnix) { FileUtils.CreateSymlink(path, Target); } else { CygwinUtils.CreateSymlink(path, Target); } }
protected void CreateSymlink(string name) { if (WindowsUtils.IsWindows) { CygwinUtils.CreateSymlink( sourcePath: Path.Combine(_sourceDirectory, name), targetPath: Contents); } else { FileUtils.CreateSymlink( sourcePath: Path.Combine(_sourceDirectory, name), targetPath: Contents); } }
/// <summary> /// Creates a symbolic link in the filesystem if possible; stores it in a <see cref="FlagUtils.SymlinkFile"/> otherwise. /// </summary> /// <param name="source">A path relative to <see cref="SubDir"/>.</param> /// <param name="target">The target the symbolic link shall point to relative to <paramref name="source"/>. May use non-native path separators!</param> protected void CreateSymlink([NotNull] string source, [NotNull] string target) { #region Sanity checks if (string.IsNullOrEmpty(source)) { throw new ArgumentNullException(nameof(source)); } if (string.IsNullOrEmpty(target)) { throw new ArgumentNullException(nameof(target)); } #endregion string sourceAbsolute = CombinePath(source); string sourceDirectory = Path.GetDirectoryName(sourceAbsolute); if (sourceDirectory != null && !Directory.Exists(sourceDirectory)) { Directory.CreateDirectory(sourceDirectory); } if (_isUnixFS) { FileUtils.CreateSymlink(sourceAbsolute, target); } else if (WindowsUtils.IsWindowsNT) { // NOTE: NTFS symbolic links require admin privileges; use Cygwin symlinks instead CygwinUtils.CreateSymlink(sourceAbsolute, target); } else { // Write link data as a normal file File.WriteAllText(sourceAbsolute, target); // Some OSes can't store the symlink flag directly in the filesystem; remember in a text-file instead string flagRelativePath = string.IsNullOrEmpty(Destination) ? source : Path.Combine(Destination, source); FlagUtils.Set(Path.Combine(TargetDir, FlagUtils.SymlinkFile), flagRelativePath); } }